关闭第二个 UIWindow 后恢复状态栏状态

Restore status bar state after closing second UIWindow

有主 UIWindow 持有 MainViewController,它使用 lightContent 作为 preferredStatusBarStyle。我创建了第二个 UIWindow 实例来显示 PopupViewController,它使用 default 作为 preferredStatusBarStyle

当我显示带有 PopupViewController 的第二个 UIWindow 时,状态栏样式更改为 default,但当我隐藏它时,样式不会变回 lightContent

同样的问题适用于我 VC 在弹出窗口 window 中隐藏状态栏的情况 - 当弹出窗口 window 关闭时状态栏不显示。

UIWindow 创建:

// Prepare window to show dialog box in
newWindow = UIWindow(frame: UIScreen.main.bounds)
newWindow?.windowLevel = 3

// Overlay new window
newWindow?.makeKeyAndVisible()
self.mainWindow.windowLevel = 1
self.mainWindow.endEditing(true)
newWindow?.isHidden = false

// Display dialog
newWindow?.rootViewController = PopupViewController()

UIWindow解雇:

UIView.animate(
    withDuration: 1.0,
    delay: 0,
    usingSpringWithDamping: 1,
    initialSpringVelocity: 0,
    options: .curveEaseOut,
    animations: { [weak self] in
        self?.newWindow?.alpha = 0
    },
    completion: { [weak self] _ in
        self?.newWindow?.windowLevel = 0
        self?.newWindow?.rootViewController = nil
        self?.newWindow?.alpha = 1
        self?.mainWindow.makeKeyAndVisible()
    }
)

谢谢!

编辑:弹出窗口随时可能出现,我不知道那个VC当时处于活动状态

更改 ViewWillAppear 上的状态栏样式

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.statusBarStyle = .lightContent
}
  1. 请在您第一个更改状态栏颜色 Light 的控制器上添加以下代码。

    override func viewDidAppear(_ animated: Bool) { UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false) }

  2. 现在,在您想要默认状态栏样式的第二个控制器中,输入代码:

    override func viewDidAppear(_ animated: Bool) { UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default, animated: false) }

通过上面的代码你一定会得到正确的解决方案

针对您的情况的简单解决方案

  1. 显示Window

针对您的情况的简单解决方案

  1. 新 UIWindow 创建:

    // Prepare window to show dialog box in
    newWindow = UIWindow(frame: UIScreen.main.bounds)
    newWindow?.windowLevel = 3
    
    // Overlay new window
    newWindow?.makeKeyAndVisible()
    self.mainWindow.windowLevel = 1
    self.mainWindow.endEditing(true)
    newWindow?.isHidden = false
    
    // Display dialog
    newWindow?.rootViewController = PopupViewController()
    // Now you can change status bar style default or other style
    UIApplication.shared.statusBarStyle = .default
    
  2. 新用户界面Window解雇:

    UIView.animate(
        withDuration: 1.0,
        delay: 0,
        usingSpringWithDamping: 1,
        initialSpringVelocity: 0,
        options: .curveEaseOut,
        animations: { [weak self] in
            self?.newWindow?.alpha = 0
            // Revert back to default
            UIApplication.shared.statusBarStyle = .lightContent
        },
        completion: { [weak self] _ in
            self?.newWindow?.windowLevel = 0
            self?.newWindow?.rootViewController = nil
            self?.newWindow?.alpha = 1
            self?.mainWindow.makeKeyAndVisible()
        }
    )
    

我要找的东西是 UIViewController.setNeedsStatusBarAppearanceUpdate()。这是告诉 VC 状态栏外观已更改且需要恢复的便捷方法。

// make main window key but transparent
self.mainWindow.alpha = 0
self.newWindow?.windowLevel = 0
self.newWindow?.alpha = 1
self.mainWindow.makeKey()

// restore status bar appearance  
self.mainWindow.rootViewController!.setNeedsStatusBarAppearanceUpdate()

// Fade in main window with (status bar is in proper state at this moment)
UIView.animate(
        withDuration: 0.9,
        delay: 0,
        usingSpringWithDamping: 1,
        initialSpringVelocity: 0,
        options: .curveEaseIn,
        animations: { [weak self] in
            self?.mainWindow.alpha = 1
        },
        completion: { [weak self] _ in
            // destroy popup VC
            self?.newWindow?.rootViewController = nil
        }
)

Here is useful article on this subject

谢谢大家!