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



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

当我显示第二个UIWindowPopupViewController状态栏样式更改为default,但是当我隐藏它时,样式不会变回lightContent

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

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处于活动状态

我一直在寻找的东西是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
}
)

这是关于这个主题的有用文章

谢谢大家!

更改 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. 新 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. 新 UIWindow 解雇:

    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()
    }
    )
    

最新更新