2

I have an app with custom colour themes, and themes can implement colours in TableView Header labels and Navigation bars.

When doing this, it was working fine with iOS14. But with changes in iOS15, the navigation bar cannot change colour anymore. The transparent and scrollEdgeAppearance was handled in iOS15, but the NavBar current-colour change based on user input while the app is running(after application(didFinisLaunching) has been called) is not working.

I am using the below code to trigger when the user selects a colour:

 func setNavBarColour() {
        if #available(iOS 15, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            appearance.backgroundColor = LangBuilds.flavColor
            let proxy = UINavigationBar.appearance()
            proxy.tintColor = LangBuilds.flavColor
            proxy.standardAppearance = appearance
            proxy.scrollEdgeAppearance = appearance
        }
        else {
            self.navigationController?.navigationBar.barTintColor = LangBuilds.flavColor
        }
        self.searchController.searchBar.searchTextField.textColor = .black
        self.searchController.searchBar.searchTextField.backgroundColor = .white
        self.searchController.searchBar.searchTextField.autocapitalizationType = .none
        changeTextHolder()
    }

Thanks for your help in advance.

1 Answers1

8

the NavBar current-colour change based on user input while the app is running(after application(didFinisLaunching) has been called) is not working

You cannot change a navigation bar color while that navigation bar is showing by using the appearance proxy. The appearance proxy affects only future interface elements. You need to apply your UINavigationBarAppearance to the existing navigation bar directly:

self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks a lot, @matt. Sorry for my dumb doubts, you've obliged me with this answer. – Jyotirmay Sharma Sep 30 '21 at 15:12
  • Plus if I'm not mistaken the code in your question is taken pretty directly from my https://stackoverflow.com/a/69302710/341994 – matt Sep 30 '21 at 15:15
  • Yes, you are right, I had a time-constrained update to push by Friday that was about solving a major bug while also supporting themes in iOS15, so I was desperately trying every solution on StackOverflow. – Jyotirmay Sharma Oct 02 '21 at 07:21