在ViewController处于纵向方向时,以横向方式在UIWebView中播放YouTube视频

时间:2011-06-03 23:58:06

标签: ios xcode youtube uiwebview landscape

我正在开发一款需要播放YouTube视频的iPhone应用程序。

我在纵向方向上有一个ViewController,只有一个加载YouTube视频的UIWebView。

我要做的是视频可以横向播放。

这听起来很简单,但我无法弄清楚如何做到这一点。有人可以帮帮我吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

这可以在AppDelegate中找到。将仅为视频播放器启用水平模式

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let presentedViewController = window?.rootViewController?.presentedViewController {

        let className = String(describing: type(of: presentedViewController))
        if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
        {
            return UIInterfaceOrientationMask.allButUpsideDown
        }

    }
    return UIInterfaceOrientationMask.portrait
}

答案 1 :(得分:0)

目前无法以编程方式开始播放YouTube视频。 也就是说,您可以在ViewController的willRotateToInterfaceOrientation中创建一个UIWebview:duration:检查水平方向,并使用显示视频缩略图的代码显示webview。但是用户仍然需要激活视图。 要删除视频,请使用相同的方法,但纵向方向并删除网页视图。

答案 2 :(得分:0)

我找到了答案。

1)我将此代码添加到我的viewController.m

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

2)在界面构建器中,我在Landscape中构建我的界面,每个元素旋转90°。

当我构建并运行时,视图似乎处于纵向模式,而实际上是横向模式。因此,UIWebView中嵌入的YouTube视频将在横向播放。