【问题标题】:Can't get supportedInterfaceOrientationsForWindow to work with Swift 1.2无法使用 Swift 1.2 获得 supportInterfaceOrientationsForWindow
【发布时间】:2015-04-16 13:47:59
【问题描述】:

嘿 Stackoverflow 成员, 也许你可以帮我解决我的问题。

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

在我使用 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) 被锁定为纵向模式。设备方向设置应该没问题!

提前感谢您的帮助!

【问题讨论】:

  • 我有两个快速建议: 1. 确保您的项目的 Info.plist 文件不包含排除横向模式的受支持界面方向的键。 2. 在 supportedInterfaceOrientationsForWindow 函数的第一个“if”语句行设置断点,然后单步执行代码以查看发生了什么。
  • 进一步了解 deanware 的 cmets - 你确定它达到了断点吗?您正在返回一个 int / 不是 UIInterfaceOrientationMask。您是否尝试过升级到 swift 1.2 菜单选项?
  • 我刚刚在一个新项目中尝试了您的 Swift 1.2 代码,它运行良好。该应用程序仅在出现 MoviePlayer 时旋转。你可能还有另一个问题。也许 XCDYoutube 不适合 iOS 8 并且不再旋转?尝试一直返回所有方向以查看电影播放器​​是否旋转。
  • 愚蠢,但和我一起,检查旋转是否锁定在我的设备中 :)

标签: ios iphone swift mpmovieplayercontroller


【解决方案1】:

我的逻辑与你的类似,但最终返回了对所有方向的支持。

在 appdelegate 中返回 UIInterfaceOrientationMaskAll。

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

  • (NSUInteger)supportedInterfaceOrientations { 返回 UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; }

【讨论】:

    【解决方案2】:

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

    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)")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2015-06-24
      • 2017-07-07
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多