DEV Community

Nicky Marino
Nicky Marino

Posted on • Originally published at nickymarino.com on

How to Change the iOS Status Bar Color with UIKit

https://nickymarino.com/public/assets/2020/optimizing-virgo/example_11.png

For some iOS apps, it may be helpful to change the color of the status bar at the top of the screen. For example, if I have a dark background, the default status bar style is hard to read:

Dark iPhone app

To change the appearance of the status bar within a view controller, first add “View controller-based status bar appearance” as an item to your Info.plist with a value of YES:

Info.plist

Then in any view controller, you override the preferredStatusBarStyle property:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Enter fullscreen mode Exit fullscreen mode

And if you ever need to update the status bar color, call setNeedsStatusBarAppearanceUpdate(). Now the full view controller looks like this:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,
        // typically from a nib.
        setNeedsStatusBarAppearanceUpdate()
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

Enter fullscreen mode Exit fullscreen mode

Running this view controller, we get a light status bar!

Dark iPhone app with light status bar

Top comments (0)