3

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?

matryx87
  • 149
  • 7
  • Maybe this helps: https://stackoverflow.com/questions/56556254/ – koen Sep 26 '19 at 23:40
  • @Koen how is that related? He isn't asking about the status bar _background_ but about the color of its _text_. – matt Sep 26 '19 at 23:42
  • @Koen Thank you, of course i checked, but as matt said it is about the color of the background more than the color of the text – matryx87 Sep 27 '19 at 07:27

1 Answers1

10

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! ;)

matryx87
  • 149
  • 7
  • It definitely worked, but want to understand more like why barStyle needs to be set and only after overrideUserInterfaceStyle – rajtharan-g Dec 20 '21 at 14:13