matryx87
matryx87

Reputation: 157

How to set style of the statusbar embedded in a navigation controller on iOS13?

as many iOS devs out there i'm facing some issues with iOS 13 update. One of these was the different management of the status bar style

On iOS 12 i used to set the navigation bar style like this

self.navigationController?.navigationBar.barStyle = .black

which affects the status bar style, setting it to white (because the navigation bar style is black); but it doesn't seem to work on iOS 13, i guess it has something to deal with

UINavigationBarAppearance() 

class

I configured my navigation bar for each ViewController like this:

if #available(iOS 13.0, *) {
            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.accessibilityTextualContext = .sourceCode
            navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.backgroundColor = .brownCircles
            navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
            navBarAppearance.shadowColor = nil // remove navigationBar Bottom border

            self.navigationController?.navigationBar.standardAppearance = navBarAppearance
            self.navigationController?.navigationBar.compactAppearance = navBarAppearance
            self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        } else {
            self.navigationController?.navigationBar.barTintColor = .blue
            self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
        }
self.navigationController?.navigationBar.barStyle = .black

so far so good, but

self.navigationController?.navigationBar.barStyle = .black

works just on iOS 12, nothing happens on iOS 13 the status bar still looks black instead of white

Did anyone face this issue?

Upvotes: 3

Views: 2189

Answers (1)

matryx87
matryx87

Reputation: 157

Finally i figured out!

the magic code to set a light status bar text is:

 self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark

of course if you want to change to dark text i have to set it to .light.

Some things to notice:

  • This code:

    if #available(iOS 13.0, *) {
        overrideUserInterfaceStyle = .dark
    }
    

    although it should set the entire view and subviews to dark, doesn't seem to affect the status bar.

  • You can also use:

    UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

    but of course is deprecated so i'd recommend other ways

  • You still need:

    self.navigationController?.navigationBar.barStyle = .black, but put it AFTER the UINavigationBarAppearance() settings and after the self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark.

Final code will look like this:

if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.accessibilityTextualContext = .sourceCode
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = .brownCircles
        navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
        navBarAppearance.shadowColor = nil // remove navigationBar Bottom border

        self.navigationController?.navigationBar.standardAppearance = navBarAppearance
        self.navigationController?.navigationBar.compactAppearance = navBarAppearance
        self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
} else {
        self.navigationController?.navigationBar.barTintColor = .blue
        self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
}
self.navigationController?.navigationBar.barStyle = .black

Hope it helps! ;)

Upvotes: 10

Related Questions