Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the status bar background color and text color on iOS 13?

With the arrival of iOS 13 statusBar's view is no longer accessible trough:

value(forKey: "statusBar") as? UIView 

Due to:

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.'

But it's not clear how it should be used for changing colours as keyWindow?.windowScene?.statusBarManager does not appear to contain anything related to it.

I'm compiling my code with (iOS 10, *) compatibility, so I intend to continue using UIKit.

Any ideas regarding this subject?

like image 903
Hugo Alonso Avatar asked Jun 18 '19 14:06

Hugo Alonso


People also ask

How do I change the color of my status bar on my iPhone?

Go to the Storyboard. Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content.

How can I change status bar background color?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.

How do I change the color of my status bar flutter on my iPhone?

If you want to change a status bar text color across your app, a theme is always the right choice for this kind of task. To do that, set brightness property in AppBarTheme to either Brightness. dark or Brightness.


Video Answer


2 Answers

You can add some conditions or use first one. Just create some extension for UIApplication.

extension UIApplication { var statusBarUIView: UIView? {     if #available(iOS 13.0, *) {         let tag = 38482         let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first          if let statusBar = keyWindow?.viewWithTag(tag) {             return statusBar         } else {             guard let statusBarFrame = keyWindow?.windowScene?.statusBarManager?.statusBarFrame else { return nil }             let statusBarView = UIView(frame: statusBarFrame)             statusBarView.tag = tag             keyWindow?.addSubview(statusBarView)             return statusBarView         }     } else if responds(to: Selector(("statusBar"))) {         return value(forKey: "statusBar") as? UIView     } else {         return nil     }   } } 

UPDATED: Sorry, I don't have enough time to test it in real projects, but it works in "Hello world" app. You can read more info about keyWindow and statusBarFrame in order to make it better.

extension UIApplication { var statusBarUIView: UIView? {      if #available(iOS 13.0, *) {         let tag = 3848245          let keyWindow = UIApplication.shared.connectedScenes             .map({$0 as? UIWindowScene})             .compactMap({$0})             .first?.windows.first          if let statusBar = keyWindow?.viewWithTag(tag) {             return statusBar         } else {             let height = keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? .zero             let statusBarView = UIView(frame: height)             statusBarView.tag = tag             statusBarView.layer.zPosition = 999999              keyWindow?.addSubview(statusBarView)             return statusBarView         }      } else {          if responds(to: Selector(("statusBar"))) {             return value(forKey: "statusBar") as? UIView         }     }     return nil   } } 
like image 175
isHidden Avatar answered Sep 20 '22 15:09

isHidden


Unfortunately Apple deprecated some of the mentioned methods of accessing the status bar and editing its attributes. You will have to use the StatusBarManager object of the WindowScene. The following method works for iOS 13 and above:

extension UINavigationController {      func setStatusBar(backgroundColor: UIColor) {         let statusBarFrame: CGRect         if #available(iOS 13.0, *) {             statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero         } else {             statusBarFrame = UIApplication.shared.statusBarFrame         }         let statusBarView = UIView(frame: statusBarFrame)         statusBarView.backgroundColor = backgroundColor         view.addSubview(statusBarView)     }  } 
like image 37
Alaa Othman Avatar answered Sep 21 '22 15:09

Alaa Othman