ios - 由于 UIInterfaceOrientationMask 在 "presentViewController"时发现 nil

标签 ios swift appdelegate uiinterfaceorientation guard

通过解决我的应用程序中的最后一个大问题,我导致了另一个可能更大的问题。我的应用程序仅支持纵向,但我希望能够以横向方式观看视频。为此,我将此方法添加到 AppDelegate:

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

    guard let vc = (window?.rootViewController?.presentedViewController) else {
        return .Portrait
    }

    if (vc.isKindOfClass(NSClassFromString("AVFullScreenViewController")!)) {
        return .AllButUpsideDown
    }

    return .Portrait
}

工作得很好。但现在,如果我提出警报或单击某些共享按钮(打开 configureMailComposeViewController() 或 UIActivityViewController()),我的应用程序就会崩溃。

fatal error: unexpectedly found nil while unwrapping an Optional value
2016-09-16 09:51:43.665772 App[1788:535789] fatal error: unexpectedly found nil while unwrapping an Optional value

我不明白这个问题,那个守卫不应该保护我免受这个问题的影响并返回.Portrait吗? 如果我删除该方法或只是返回 .Portrait,除了横向观看视频之外,一切都会再次正常工作。

请不要建议将我的应用程序设置为横向方向除外,并为每个 Controller 禁用它。我尝试过,但不适用于我的情况。

最佳答案

在您的AVFullScreenViewController中,当此 View Controller 弹出或关闭时,将窗口状态栏的界面方向设置为纵向。您可以在AVFullScreenViewControllerviewWillDisappear中进行管理。

关于ios - 由于 UIInterfaceOrientationMask 在 "presentViewController"时发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526627/

相关文章:

iOS:使用 NSUrlConnection 使用 RESTFUL Web 服务时出现 415(不支持的媒体类型)错误

ios - 如何创建 .dSYM 文件?

ios - iOS 是否允许将用户重定向到网站进行付款?

ios - 从React Native App内的AppDelegate.m访问/接收值

ios - 从 AppDelegate 和 ViewController 访问 locaitonManager

ios - 使用 JSON 数据填充 UILabel 并编码 URL

iphone - 如何更改 UIButton 标题?

swift - 在 swift 中使用 RegExp 降低浮点精度

swift - TableView 在 Swift 中永久重新排序

iOS self.window - 它是什么时候创建的?