How do I change the color of the status bar with a button?

I am trying to implement a button that changes the background of my application from white to black along with the status bar color. Right now I have

@IBAction func buttonTapped(_ sender: Any) {
        self.view.backgroundColor = UIColor.black
        UIApplication.shared.statusBarStyle = .lightContent
}

      

This does not work. Does anyone know a way to change the font color of the status bar without reloading the whole view?

+3


source to share


1 answer


If you want to set the style of the status bar, then set the application level UIViewControllerBasedStatusBarAppearance

to NO

in the `.plist 'file.

if you want to set the style of the status bar, at the controller level, do the following:

  • Set UIViewControllerBasedStatusBarAppearance

    to YES

    in file .plist

    if you only need to style the status bar at the UIViewController level.
  • In addDidLoad add function - setNeedsStatusBarAppearanceUpdate

  • override the preferredStatusBarStyle in your view controller.



-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

      

Set the .plist value according to the level of customization of the status bar style. enter image description here

+1


source







All Articles