1
votes

I have a UITabBarController as the root view controller. Each tab has a view controller embedded inside of a navigation controller. My navigation bars are a dark color so I need the status bar to be set to .lightContent. I also need to hide the status bar dynamically.

If I set "View controller-based status bar appearance" to "NO", I can set the status bar correctly to ".lightContent", but I cannot hide the status bar dynamically.

If I set "View controller-based status bar appearance" to "YES", the status bar will only set to ".lightContent" if a navigation bar is NOT present.

This is what I use in each view controller to show or hide the status bar:

var shouldHideStatusBar: Bool = false {
    didSet { self.setNeedsStatusBarAppearanceUpdate() }
}
override var prefersStatusBarHidden: Bool { return shouldHideStatusBar }
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { return .slide }
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }

I have also tried:

navigationController?.navigationBar.barStyle = .black

There must be something I am missing in order to set the status bar to .lightContent and still be able to dynamically show and hide the status bar.

2

2 Answers

0
votes

Resulted to using a deprecated method:

UIApplication.shared.setStatusBarHidden(true, with: .slide)

Not ideal, but works. Hopefully, someone has a solution without relying on a deprecated method.

0
votes

You have to bubble these values up through your view hierarchy. For instance, your tabBarController should be asking its selectedViewController what its value for prefersStatusBarHidden is then your navigationController needs to ask its topViewController what its value for prefersStatusBarHidden is. This way when prefersStatusBarHidden is called on the tabBarController it relays back what the top most view controller wants to do with the status bar.