Ios Can';t获得Windows界面方向的支持,以便与Swift 1.2一起使用

Ios Can';t获得Windows界面方向的支持,以便与Swift 1.2一起使用,ios,iphone,swift,mpmovieplayercontroller,Ios,Iphone,Swift,Mpmovieplayercontroller,嘿,各位成员, 也许你能帮我解决我的问题 问题是我想将所有UIViewController的方向锁定为“纵向”,但如果电影播放器出现,则应切换到横向模式,如果电影播放器消失,则应切换回横向模式 在Swift 1.2之前,我使用: func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> UIInterfaceOrientationMask

嘿,各位成员, 也许你能帮我解决我的问题

问题是我想将所有UIViewController的方向锁定为“纵向”,但如果电影播放器出现,则应切换到横向模式,如果电影播放器消失,则应切换回横向模式

在Swift 1.2之前,我使用:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> UIInterfaceOrientationMask {
//If the video is being presented, let the user change orientation, otherwise don't.
if let presentedViewController = window.rootViewController?.presentedViewController? {
    if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
        return .AllButUpsideDown
    }
}
return .Portrait
}
使用Swift 1.2时,有些事情发生了变化,因此我最终得到了以下代码:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    //If the video is being presented, let the user change orientation, otherwise don't.
    if let presentedViewController = window?.rootViewController?.presentedViewController {
        if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        }
    }
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
但是我的代码不起作用,电影播放器(XCDYoutube)被锁定在纵向模式。设备方向设置应该可以


提前感谢您的帮助

我的逻辑与你的相似,但最终还是支持所有方向

在appdelegate中返回UIInterfaceOrientationMaskAll

根据您拥有的视图控制器数量,您可能希望创建UIViewController的抽象子类,并仅返回对纵向/的支持,然后攻击youtube视图控制器以支持横向

  • (整数)支持的接口方向{ 返回UIInterfaceOrientationMASKLAND SCAPELEFT | UIInterfaceOrientationMASKLAND SCAPERIGHT; }

    • 我刚刚遇到了完全相同的问题。我找到了一种通过到达控制器堆栈顶部来修复它的方法:

      func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
          //If the video is being presented, let the user change orientation, otherwise don't.
          if var presentedViewController = window?.rootViewController?.presentedViewController {
              // Get the controller on the top of the stack
              while (presentedViewController.presentedViewController) != nil {
                  presentedViewController = presentedViewController.presentedViewController!
              }
      
              if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
                  return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
              }
          }
          return Int(UIInterfaceOrientationMask.Portrait.rawValue)
      }
      
      您还可以尝试显示“presentedViewController”的类型,以确保它是正确的:

      println("presentedViewController type: \(presentedViewController.dynamicType)")
      

      我有两个简单的建议:1。确保项目的Info.plist文件不包含排除横向模式的支持界面方向键。2.在SupportedInterfaceOrientionsForWindow函数中的第一个“if”语句行设置断点,然后单步遍历代码以查看发生了什么。关于deanware的注释,您确定它达到了断点吗?您正在返回一个int/not UIInterfaceOrientationTask。你尝试升级到swift 1.2菜单选项了吗?我刚刚在一个新项目中尝试了你的swift 1.2代码,效果很好。应用程序仅在播放电影播放器时旋转。你可能还有另一个问题。也许XCDYoutube在iOS 8上玩得不好,不再旋转了?尝试始终返回所有方向以查看电影播放器是否旋转。愚蠢,但与我一起检查旋转是否锁定在我的设备中:)