4
votes

I'm trying to change the status bar text to white on some of the view controllers, but on a few others it still needs to be black. (so cannot change it globally for the whole app).

Everything works fine, except the first few seconds, when clicking on different tab bars, when there is a glitch, and half of the status bar text becomes black, instead of white. Then everything updates perfectly.

The structure is like this: A tab bar controller with a few tabs, each tab containing a navigation controller, containing a view controller.

Having navigation controllers made it necessary to change the navigationController?.navigationBar.barStyle = .black to make it work.

After pressing a few times on tabs, it works fine.

First thing I did of course was to set to YES the View controller-based status bar appearance from the .plist

I also have override the preferredStatusBarStyle, using a boolean to set which view controllers should have the white or black status bar text:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return coloredNavigationBar ? .lightContent : .default
}

var coloredNavigationBar: Bool = true

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if coloredNavigationBar {
        navigationController?.navigationBar.barStyle = .black
    } else {
        navigationController?.navigationBar.barStyle = .default
    }
    setNeedsStatusBarAppearanceUpdate()
}

The code pasted is from the base view controller used everywhere.

SCREENSHOT - status bar glitch demo

1
Cool! But how do we reproduce this? What does "when clicking on different tab bars" mean? There are no "tab bars" in your screen shot or your code.matt
The structure is like this: A tab bar controller with a few tabs, each tab containing a navigation controller, containing a view controller. Having navigation controllers made it necessary to change the .barStyle to make it work. After pressing a few times on tabs, it works fine. The code pasted is from the base view controller used everywhere.Irina DM
Okay, so the problem is that in navigation controller, your preferredStatusBarStyle is not called. A navigation controller gets the status bar style from the navigation bar style. But also, you've got a tab bar controller as your top level view controller, so it gets to say what the status bar style is. You should probably be giving the tab bar controller a delegate so it can set the status bar style as needed.matt

1 Answers

1
votes

Yes! @matt was right, thank you. Indeed iOS gets confused when using a tabbar.

The solution was to add this to the TabBarController:

override public var childForStatusBarStyle: UIViewController? {
    if let controller  = selectedViewController as? UINavigationController {
        return controller.visibleViewController
    }
    return selectedViewController
}