【问题标题】:Swift - How to enable landscape in full screen video?Swift - 如何在全屏视频中启用横向?
【发布时间】:2015-08-20 05:52:11
【问题描述】:

我正在开发一个 iPhone(Swift) 应用程序,我有一个加载 Youtube 视频的 WebView,我希望仅在视频全屏时允许横向模式。我的整个应用程序是当视频未全屏播放时,纵向和旋转被禁用。当 youtube 视频不是全屏时,我不希望用户能够将应用程序旋转为横向,只有当视频全屏播放时。

我尝试了以下代码,但它不起作用。

override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

【问题讨论】:

    标签: ios swift webview youtube landscape-portrait


    【解决方案1】:

    supportedInterfaceOrientations 在我的情况下也不是一个可靠的解决方案。我所做的是在应用程序委托application supportedInterfaceOrientationsForWindow 方法中获取当前视图控制器,并检查该控制器是否是支持所有方向的视图控制器,因此它的返回值是根据此条件计算的

     func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    
        if let currentVC = getCurrentViewController(self.window?.rootViewController){
    
            //VideoVC is the name of your class that should support landscape
            if currentVC is VideoVC{
    
                return Int(UIInterfaceOrientationMask.All.rawValue)
            }
        }
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }
    
    func getCurrentViewController(viewController:UIViewController?)-> UIViewController?{
    
         if let tabBarController = viewController as? UITabBarController{
    
            return getCurrentViewController(tabBarController.selectedViewController)
        }
    
        if let navigationController = viewController as? UINavigationController{
            return getCurrentViewController(navigationController.visibleViewController)
        }
    
        if let viewController = viewController?.presentedViewController {
    
            return getCurrentViewController(viewController)
    
        }else{
    
            return viewController
        }
    }
    

    【讨论】:

    • 我应该把它放在我的 AppDelegate 还是我的 viewController 中?
    • 谢谢。现在只有一个错误,请检查:postimg.org/image/5rntzarzz
    • 已修复,再次检查
    • 再次感谢。这次没有错误,但是当我加载应用程序时应用程序崩溃:postimg.org/image/yqwd7jvw7
    • 这次没有错误和崩溃,但是当我打开 youtube 视频时,我无法将其旋转到横向
    【解决方案2】:

    我最近遇到了这个问题,并使用Zell B. 的答案,这是一个 Swift 5 版本,它将检测 AVPlayer 并更新方向设置。

    只需将以下代码复制并粘贴到您的 AppDelegate 中(无需编辑):

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    
        if let currentVC = getCurrentViewController(self.window?.rootViewController), currentVC is AVPlayerViewController {
            return .allButUpsideDown
        }
        return .portrait
    }
    
    func getCurrentViewController(_ viewController: UIViewController?) -> UIViewController? {
    
        if let tabBarController = viewController as? UITabBarController {
            return getCurrentViewController(tabBarController.selectedViewController)
        }
    
        if let navigationController = viewController as? UINavigationController{
            return getCurrentViewController(navigationController.visibleViewController)
        }
    
        if let viewController = viewController?.presentedViewController {
            return getCurrentViewController(viewController)
        }
        
        return viewController
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多