状态栏不遵循应用程序的方向并旋转

5

我有一个根视图控制器,管理其内部的所有其他控制器。因此,我重写了根视图控制器的shouldAutorotate和supportedInterfaceOrientations方法,代码如下:

public override func shouldAutorotate() -> Bool {
    if let vc = view.window?.rootViewController?.presentedViewController {
        if NSStringFromClass(vc.classForCoder) == "AVFullScreenViewController" {
            return true
        }
    }
    return false
}

public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if let vc = view.window?.rootViewController?.presentedViewController {
        if NSStringFromClass(vc.classForCoder) == "AVFullScreenViewController" {
            return UIInterfaceOrientationMask.AllButUpsideDown
        }
    }
    return UIInterfaceOrientationMask.Portrait
}

我的应用程序只支持竖屏,除非使用AVPlayerViewController观看全屏视频。

上面的代码在整个应用程序中都很好地工作。我所有的控制器和它们的视图都保持在竖屏状态,当用户全屏拍摄视频时,它会自动旋转到横屏模式,没有任何问题。

我的问题是,竖屏模式下的状态栏没有遵守方向掩码而旋转到了横屏模式,但在横屏模式下状态栏却被隐藏了。另一个奇怪的问题是,当应用程序处于横屏模式时,控制中心和通知中心现在可以像应用程序在横屏模式下一样被滑开。

最小支持的iOS版本是9.0。有什么建议吗?

1个回答

3
我最终删除了我的根视图控制器中的上述代码,并将其添加到我的应用程序委托中。状态栏现在保持不动。
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    if let vc = window?.rootViewController?.presentedViewController {
        if NSStringFromClass(vc.classForCoder) == "AVFullScreenViewController" {
            return UIInterfaceOrientationMask.AllButUpsideDown
        }
    }
    return UIInterfaceOrientationMask.Portrait
}

1
不确定为什么,但在我的情况下函数参数 window 是 nil。因此我使用 self.window 访问 UIApplication 的 window 属性。 - Vitalii Gozhenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接