Search code examples
iosswift3statusbar

Status Bar Style - Swift 3 - change at any time


I'm finding it hard to change the status bar style programatically.

I see how to statically set it for each ViewController using a combo of (in ViewController.swift):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.default
}

and (in info.plist):

View controller-based status bar appearance = YES

...

I'm looking to change it whenever I want!


Solution

  • Found the answer after quite a lot of digging!

    Set (in info.plist):

    View controller-based status bar appearance = NO
    

    and remove the (in ViewController.swift):

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIStatusBarStyle.default
    }
    

    ...

    Now you can use (in ViewController.swift):

    UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
    

    And, to initially set the style for each ViewController, use viewDidAppear:

    override func viewDidAppear(_ animated: Bool) {
        UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
    }