iOS Swift TabBar status bar text color glitch half white half black

Viewed 241

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 Answers

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
}
Related