【发布时间】: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