Home > Software design >  How to change navigation bar & back button colour iOS 15
How to change navigation bar & back button colour iOS 15

Time:09-24

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

CodePudding user response:

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

CodePudding user response:

Add the following code in your initialization of view controller(s):

if #available(iOS 14.0, *) {
    navigationItem.backButtonDisplayMode = .minimal
}

Here is a helpful article as well: https://sarunw.com/posts/new-way-to-manage-back-button-title-in-ios14/

  • Related