简体   繁体   中英

How to changes status bar colour of second UIWindow?

I have added the second window in my app for presenting the specific controller when user taps the remote push notification.

func navigateThroughPushnotification(controller: UINavigationController) {
    let window = UIWindow(frame: UIScreen.main.bounds)
    let vc = UIViewController()
    vc.view.backgroundColor = .clear
    window.rootViewController = vc
    window.windowLevel = UIWindow.Level.alert + 1
    window.makeKeyAndVisible()
    if let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
        statusBar.backgroundColor = kAppDefaultColor
    }
    vc.present(controller, animated: true, completion: nil)
}

Problem is that I want to change the status bar color of this window also but the code which I have used to change the color of my main window doesn't work this time.

guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return true }
    statusBar.backgroundColor = kAppDefaultColor

Now this code works very well for the main window. Please someone help to detect what should be done to change the color of the second window. Thanks in advance

This is Used for only iOS 7 and higher version

First of all go to your application's Info.plist, set View controller-based status bar appearance to YES.

after override preferredStatusBarStyle in each of your view controllers.

eg:

override var preferredStatusBarStyle: UIStatusBarStyle {     
      return .lightContent
}

If you have preferredStatusBarStyle returning a different preferred status bar style based on something that changes inside of your view controller, then you want to call setNeedsStatusBarAppearanceUpdate() on state changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM