Search code examples
swiftuikituinavigationbarios15uinavigationbarappearance

How to change navigation bar & back button colour iOS 15


I have UIkit project and I want to change navigation bar colour and back button colour.It is working fine on previous versions. but not in iOS 15. I put following code on AppDelegate,It is change the Title colour but not back button item colour.How to fix it?

if #available(iOS 15.0, *) {
   let appearance = UINavigationBarAppearance()
   let navigationBar = UINavigationBar()
   appearance.configureWithOpaqueBackground()
   appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
   appearance.backgroundColor = .red
   navigationBar.tintColor = .white
   navigationBar.standardAppearance = appearance;
   UINavigationBar.appearance().scrollEdgeAppearance = appearance
}else{
   let navBarAppearnce = UINavigationBar.appearance()
   navBarAppearnce.tintColor = .white
   navBarAppearnce.barTintColor = .red
   navBarAppearnce.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
}

Output


Solution

  • These lines are totally pointless:

    let navigationBar = UINavigationBar()
    navigationBar.tintColor = .white
    navigationBar.standardAppearance = appearance
    

    You are creating a navigation bar, configuring it, and throwing it away. That does nothing for your app. Rewrite meaningfully:

        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        appearance.backgroundColor = .red
        let proxy = UINavigationBar.appearance()
        proxy.tintColor = .white
        proxy.standardAppearance = appearance
        proxy.scrollEdgeAppearance = appearance