UINavigationBar background color not changing for a specific view

39 Views Asked by At

I have a global setting at the start of the app that makes the navigation bar white with black buttons like this:

        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundColor = UIColor.white

            let buttonAppearance = UIBarButtonItemAppearance()
            buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.black]
            appearance.buttonAppearance = buttonAppearance
            appearance.doneButtonAppearance = buttonAppearance

            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().tintColor = UIColor.black
            UINavigationBar.appearance().shadowImage = UIImage()
            UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        } else {
            UINavigationBar.appearance().backgroundColor = UIColor.white
            UINavigationBar.appearance().shadowImage = UIImage()
            UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        }

It works well, now the issue I have one specific viewcontroller where I want the navigation bar to be another color but nothing seems to work. I tried all of the following in viewDidLoad :

            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundColor = .red //1

            let buttonAppearance = UIBarButtonItemAppearance()
            buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
            appearance.buttonAppearance = buttonAppearance
            appearance.doneButtonAppearance = buttonAppearance

            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance

self.navigationController?.navigationBar.barTintColor = .red //2

self.navigationController?.navigationBar.backgroundColor = .red //3

I checked the various questions related to navigationbar colors but they all seems to suggest the same things that don't work.

Using Xcode 15.2 iOS 17.2

1

There are 1 best solutions below

0
matt On

The UINavigationBar.appearance() proxy is global and affects all navigation bars that have not been created yet. Neither of those is what you want. You want to affect just one navigation bar, which does already exist. So in your second code, instead of

UINavigationBar.appearance().standardAppearance = appearance

and so on, say

navigationItem.standardAppearance = appearance

and so on. This will set the appearance of your navigation bar just in the case where this view controller is appearing.