Search code examples
iosuinavigationcontrolleruinavigationbaruistatusbar

UIStatusBar style consistent with UINavigationBar


Just to study the UI of UINavigationBar and UIStatusBar, I changed the Navigation Bar Style to Black, and unchecked the Bar visibility, i.e. Shows Navigation Bar, but the UIStatusBar style didn't change to lightContent.

It seems that if we hide the navigation bar, the status bar style doesn't change depending on the navigation bar's style, it always takes the default - i.e. black - text and we have to change the status bar style explicitly.

Is there any method or variable we need to set to keep it dependent on the navigation bar style even if it's hidden?


Solution

  • As to my knowledge, there is no built-in setting that automatically detects the navigation bar's visibility and changes the status bar accordingly. If you want to achieve that, override preferredStatusBarStyle in your view controller:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        guard let navBarStyle = navigationController?.navigationBar.barStyle else {
            return .default
        }
    
        switch navBarStyle {
        case .black, .blackTranslucent: return .lightContent
        default: return .default
        }
    }
    

    As this answer describes, you should set the View controller-based status bar appearance in your Info.plist to YES.