Overflowed
Overflowed

Reputation: 154

How do you change the colour of the status bar with a button?

I'm trying to implement a button that changes the background of my app from white to black along with the colour of the status bar. Right now I have

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

This doesn't seem to work. Does anyone know of a way of changing the colour of the status bar font without reloading the entire view?

Upvotes: 3

Views: 928

Answers (1)

Krunal
Krunal

Reputation: 79756

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file.

if you wan to set status bar style, at view controller level then follow these steps:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

-

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

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

Upvotes: 2

Related Questions