Anuranjan Bose
Anuranjan Bose

Reputation: 231

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.

Upvotes: 1

Views: 79

Answers (2)

Tom
Tom

Reputation: 543

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
}

Upvotes: 3

Ram
Ram

Reputation: 804

@Anuranjan Bose Try this on your view did load,

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

Upvotes: 1

Related Questions