-1

One of my Controller is having navigation status bar color is black, I want to make it white. how can I change it? enter image description here

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
Aakash Gupta
  • 184
  • 3
  • 9

5 Answers5

2

As some people already suggested, inside your viewController set:

override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }

But, this is not enough. Since that viewController is contained inside UINavigationController, you need to say to nav controller to use status bar style based on currently displayed controller. One way to do this is by extending UINavigationController like this:

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }
}
Predrag Samardzic
  • 2,661
  • 15
  • 18
1

The preferredStatusBarStyle property is set to lightContent. Build and Run the project to see the content of the status bar changed to light.

override var preferredStatusBarStyle: UIStatusBarStyle {     
      return .lightContent
}

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.

  • To change the style of the navigation controller to lightinside the app, add the following viewDidAppear(_:) method
override func viewDidAppear(_ animated: Bool) {
    navigationController?.navigationBar.barStyle = .black
}
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
0

Add viewWillAppear to your code

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.white
    }
    let img = UIImage()
    navigationController?.navigationBar.shadowImage = img
    navigationController?.navigationBar.setBackgroundImage(img, for: UIBarMetrics.default)
    navigationController?.navigationBar.backgroundColor =  UIColor.white
    navigationController?.navigationBar.barTintColor = UIColor.white
}
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
  • This code doesn't work with the following error: `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. ` – yannisalexiou Oct 23 '22 at 14:52
-1

Call this inside your ViewController:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Updated for Swift 5:

override var preferredStatusBarStyle: UIStatusBarStyle {
    .lightContent
}

Also, if you don't already have made it, you should set the View controller-based status bar appearance property from your info.plist file to YES.

Jordi Gámez
  • 3,400
  • 3
  • 22
  • 35
-1

By default we can set Status bar Style in our project plist but if you want to define color for specific controller then we should override following method in your controller class .

override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }

Hope it will help you :)

Aakash Gupta
  • 184
  • 3
  • 9