Search code examples
swiftuinavigationbarios-darkmode

Manually switch NavigationBar to Dark Mode


In my app I have a manual switch to go between light/dark modes and what I'm trying to accomplish is to get the navigation bar to have the "dark mode" appearance (White text/icons and black background) triggered when I need to switch between light/dark.

I have tried everything below:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor=UIColor.white

self.navigationController?.navigationBar.barTintColor=UIColor.white
self.navigationController?.navigationBar.titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.white]

And the navigation bar never changes when any of the code above I have tried is entered.

What is the correct way to accomplish this?


Solution

  • This is what ended up working for me:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func viewDidAppear(_ animated: Bool) {
        navigationController?.navigationBar.barStyle = .black
    }