Search code examples
swiftstatusbarios13swift5

What is the correct way to manage statusBarStyle in Swift?


I've tried adding key UIViewControllerBasedStatusBarAppearance to true inside info.plist file and then added the below code inside UINavigationController class which holds several UIViewController classes.

class HomeNavigationController: UINavigationController {

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

But, it did not work.

I've also tried setting the barStyle property of navigationBar to .black but that too didn't work either.

Also looked up to https://stackoverflow.com/a/58203998/9180494, but that did not help as well.

Please NOTE: For UIViewController classes not embedded inside any UINavigationController , if I use computed property preferredStatusBarStyle, then it works.


Solution

  • Try in viewDidAppear() of UINavigationController class:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.navigationBar.barStyle = .black
    }
    

    Also add (in the same class as above):

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }