1

How can I change Swift to set StatusBar background color?

Before this code worked es expected.

func setStatusBarStyle(_ style: UIStatusBarStyle, backgroundColor:UIColor = .clear) {
     if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
         statusBar.backgroundColor = backgroundColor
         statusBar.setValue(style == .lightContent ? UIColor.white : .black, forKey: "foregroundColor")
     }
}

and now with Xcode11 I get compile Fatal Error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

any thought would be appreciated

Hattori Hanzō
  • 2,349
  • 4
  • 19
  • 36
  • Did you try statusBarManager ? – Nikola Lukic Sep 23 '19 at 14:07
  • @NikolaLukic yes but not clear solution for now – Hattori Hanzō Sep 24 '19 at 09:36
  • This is good point for you lessons. You must learn how to resolve bug. First if you got error message that is very good. Always follow errors log. In this case you will need api doc for statusBarManager. Make research on google. here is it : https://developer.apple.com/documentation/uikit/uiwindowscene/3213943-statusbarmanager. . Looks like this call only return status no "set new status" ... etc. You need override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent // .default } – Nikola Lukic Sep 24 '19 at 11:31
  • @NikolaLukic yes I did this but what about StatusBar's background color, this is old Project and I needed quick fix – Hattori Hanzō Sep 24 '19 at 13:43

0 Answers0