Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change status bar background color in iOS13

With iOS13 I cannot change the background color of the statusbar anymore because the statusbar is no longer accessible using "value for key". Did anybody figure out how this is possible or is there any knowledge that it will be possible in the final version of iOS13?

I came across different proposals already like using the UIApplications StatusBarView (no longer accessible in xcode 11, beta 7) or using the statusbarmanager. Both do not give access to the status bar.

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
  statusBar.backgroundColor = <Some Color>
}

I expect the status bar to get the background color that I need.

like image 568
Nij Avatar asked Aug 30 '19 12:08

Nij


People also ask

Is there a way to change the status bar background in iOS?

'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead. I think the simplest way is to use NavigationController instead of ViewController. Changing of Navigation Bar background using storyboard will also reflect on status bar

Is StatusBar's view no longer accessible with the arrival of iOS 13?

With the arrival of iOS 13 statusBar's view is no longer accessible trough: value (forKey: "statusBar") as? UIView 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.

Why is the status bar dark in my iOS project?

Build and Run the project, The content of the status bar is dark again, which is the default. The reason for this is, iOS asked for the style of the status bar of the navigation controller instead of the contained view controller.

How do I change the color of the status bar?

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. The style of the status bar can be changed to a status bar with white content.


3 Answers

Objective-C Version using UIStatusBarManager:

UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar]
like image 80
Lukas Avatar answered Oct 09 '22 13:10

Lukas


Use this on your preferred ViewController

override func viewDidAppear(_ animated: Bool) {
    if #available(iOS 13, *)
    {
        let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
        statusBar.backgroundColor = UIColor.systemBackground
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    }
}

you can use your Color in "UIColor.systemBackground"

like image 6
Dante Avatar answered Oct 09 '22 12:10

Dante


Code works for Swift 5+ and iOS 13+

To change the StatusBar background colour, i have created a base class of UIViewController so that i can inherit same code in all the view controllers.

Added "statusBarColorChange()" to UIViewController extension.

extension UIViewController {

  func statusBarColorChange() {

    if #available(iOS 13.0, *) {

        let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = .appNavigationThemeColor
            statusBar.tag = 100
        UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)

    } else {

            let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = .appNavigationThemeColor

        }
    } }

Created Base Class and inherited in other classes.

class BaseClass: UIViewController {

   override func viewWillAppear(_ animated: Bool) {
       self.statusBarColorChange()
   }    
}

class ChatListViewController: BaseClass {} 

Hope will be helping :)

like image 6
JaspreetKour Avatar answered Oct 09 '22 13:10

JaspreetKour