在单独的 UIWindow 中锁定状态栏方向

Lock status bar orientation in separate UIWindow

我有一个案例需要在我的应用程序中创建一个新的 UIWindow。我还希望新的 window 被锁定为纵向,但我的应用程序的原始 window 应该 NOT 被锁定为纵向。

我正在像这样设置我的新 window:

newWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
newWindow?.rootViewController = ViewController2()
newWindow?.makeKeyAndVisible()

并且 ViewController2 锁定方向:

override var shouldAutorotate: Bool {
    return false
}

问题是,当显示新的 window 并且用户旋转设备时,应用程序本身会锁定在纵向模式,但状态栏会旋转到横向模式:

如何确保状态栏在我的新 window 中也锁定为纵向?

如果有人想在一个简单的项目中看到它的实际效果:https://github.com/rajohns08/StatusBar

使用此文档解决您的问题 https://developer.apple.com/reference/uikit/uiinterfaceorientationmask

或 解决方案在应用程序委托中。您可以分别处理每个 window 支持的方向,那里:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
   // check, which window is asking and return the mask
   return UIInterfaceOrientationMaskAllButUpsideDown;
}