nintyapple
nintyapple

Reputation: 237

UINavigationBar tint color does not update

I am implementing a dark mode in my app. Here is my code (that I call when the screen is double tapped):

if darkMode == false {
UINavigationBar.appearance().tintColor = UIColor(hexString: "#3A3A3A")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
} else {
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
UINavigationBar.appearance().barTintColor = UIColor(hexString: "#FFFDF3")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()]

The only thing that updates is my status bar, but the navigation bar does update after I go into another view and return back to the main view. Why is that? Is there something I'm doing wrong?

Upvotes: 3

Views: 1934

Answers (2)

nintyapple
nintyapple

Reputation: 237

Got it. You can't change appearance() at runtime, but you can just do navigationController?.navigationBar.tintColor = UIColor.redColor()

Upvotes: 1

libec
libec

Reputation: 1554

I was just dealing with the same issue, turns out if you change appearance() proxy at runtime it doesn't have any effect. You need to change directly the properties of instances. So what you need to do is have subclassed UINavigationBarController with method where you set the colors and status bar appearance, for instance:

class ColorNavigationController: UINavigationController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        setupForColor(UIFactory.sharedInstance.tintColor) //provides default color
    }

    func setupForColor(color: UIColor) {
        navigationBar.tintColor = color
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

    }
}

Then when you double tap the screen:

if let colorNavigationController = self.navigationController as? ColorNavigationController {
    colorNavigationController.setupForColor(UIColor.redColor) // based on your current scheme
}

Upvotes: 1

Related Questions