Home > Mobile >  Change current colour of NavigationBar in iOS15
Change current colour of NavigationBar in iOS15

Time:10-01

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.

CodePudding user response:

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
  • Related