Change color of Back button in navigation bar
Asked Answered
J

31

259

I am trying to change the color of the Settings button to white, but can't get it to change.

I've tried both of these:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

but no change, it still looks like this:

enter image description here

How do I make that button white?

Joniejonina answered 26/2, 2015 at 3:27 Comment(1)
It's already here, you can check the below link: https://mcmap.net/q/111194/-change-color-and-back-button-title-of-navigation-bar-from-swift-3-codeGauguin
H
331

You can change the global tint color in your storyboard by clicking on an empty space on the board and select in the right toolbar "Show the file inspector", and you will see in the bottom of the toolbar the "Global Tint" option.

Global Tint option in storyboard

Heroworship answered 2/5, 2015 at 10:43 Comment(9)
woah.. Nice find!. do you know how to do this programmatically?Barram
Great find, but changing global tint to white (per the original question) will also change things like table view detail disclosure indicators to white, which would make them invisible in the table in question.Cobbs
Adding on to Mike's issues: this control also makes all your text buttons this color and so you'll either have to set them to a color by hand (ie no default colors obviously) or just use a different method.Kepi
While this is a quick and easy fix, it leads to other problems if the color is white/matches the background. If you have text fields, it makes the cursor unnoticeable, which makes it look to the user as if they are not able to write on a text field because the cursor isn't visible.Commandant
This actually fixed an issue I was having with my Back Button not showing up when performing segue's within Navigation Controllers - it was because my global tint was transparent! Thanks for this, it helped identify the issue :)Trachoma
@PaulLehn To do this programmatically, in AppDelegate.swift > application(application:didFinishLaunchingWithOptions:) you can write: self.window!.tintColor = .yourColorLatrice
Be aware though that this is impossible to do if your view controller is only a XIB and not part of a Storyboard. In that case, you'll have @deepak's answer and set it programmatically.Beamends
Correct answer is actually down a few: https://mcmap.net/q/109218/-change-color-of-back-button-in-navigation-barBemba
This worked, as soon as I removed the overridden code segment self.navigationBarView.tintColor = UIColor(ciColor: Constant.DefaultThemeColor) that has been implemented in main navigation controllerBiogeochemistry
M
249

This code changes the arrow color

self.navigationController.navigationBar.tintColor = UIColor.whiteColor();

If this does not work, use the code below:

self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()

Swift 3 Notes

UIColor.whiteColor() and similar have been simplified to UIColor.white

Also, many previously implicit optionals have been changed to explicit, so you might need:

self.navigationController?.navigationBar =
Melly answered 26/2, 2015 at 4:6 Comment(5)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() worked for me (Swift 2.2)Cobbs
self.navigationBar.barStyle = UIBarStyle.Black self.navigationBar.tintColor = UIColor.whiteColor() worked for me (Swift 3)Shuman
Can you explain why to use UIBarStyle.Black?Latrice
Please update: self.navigationController?.navigationBar.tintColor =Neman
it only changes the arrow, back text still remains the same in Xcode 11Waterproof
A
106

Very easy to set up in the storyboard:

enter image description here

enter image description here

Apologue answered 22/6, 2016 at 5:33 Comment(0)
S
69

You should use this:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
Sarcomatosis answered 26/2, 2015 at 4:1 Comment(2)
This made the button white, but made the background color much lighter. Do you know how to give the bar's background its full color again?Joniejonina
This is the correct answer. It will make the bar purple, by setting the barTintColor and the title / bar buttons white.Voltz
I
49

Swift

 override func viewDidLoad() {
     super.viewDidLoad()

 self.navigationController?.navigationBar.tintColor = UIColor.white
 }
Isocyanide answered 23/6, 2017 at 11:10 Comment(7)
Use in swift 3.Isocyanide
also tell me which swift version you can use?Isocyanide
And which class/file/vc in write this code in project?Isocyanide
At the beginning of function setCloseButton() which is called on viewWillAppear.Vic
write in NextViewController.swift file, also write in viewDidLoad().Isocyanide
Let us continue this discussion in chat.Isocyanide
You can change this color via the xCode interface or how do you call it.. So select your navigation controller and at your right select the Attribute selector (4th option) then go to the 3th section where it says Large Title Text Attribute and change the color over there, this did it for me.Galloping
P
32

Swift 5.5

Change complete app theme

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     // Set for app
     UINavigationBar.appearance().tintColor = .white
     return true
}

Change specific controller

let navController = UINavigationController.init(rootViewController: yourViewController) 
navController.navigationBar.tintColor = .red

present(navController, animated: true, completion: nil)
Preciosity answered 12/9, 2018 at 8:4 Comment(3)
it changes the color for all the application. What if I need to change it for particular screen/particular UINavigationController?Waal
@VyachaslavGerchicov above answer is for complete app theme, if you want to change specific controller. Do something like this: let navController = UINavigationController.init(rootViewController: yourViewController) navController.navigationBar.tintColor = .redPreciosity
If you are on iOS 13 you don't use didFinishLaunchingWithOptions anymore, it was moved to SceneDelegate.Crural
Z
21

You can use like this one. Place it inside AppDelegate.swift.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().translucent = false
        UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

        return true
    }
Zacharyzacherie answered 3/5, 2015 at 19:4 Comment(0)
T
15

In Swift3, To set the Back button to red.

self.navigationController?.navigationBar.tintColor = UIColor.red
Tarter answered 26/1, 2017 at 14:56 Comment(4)
this will set the navigation bar, not the bar button itemGoogol
Yep, that was the question.Tarter
"Change color of Back button in navigation bar"Googol
This was the only method that worked for me. Something seems broken with Apple's implementation right now...Loris
F
14

In Swift 4, you can take care of this issue using:

let navStyles = UINavigationBar.appearance()
// This will set the color of the text for the back buttons.
navStyles.tintColor = .white
// This will set the background color for navBar
navStyles.barTintColor = .black
Floorman answered 27/12, 2017 at 18:23 Comment(0)
H
14
    self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation 
    self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color
Held answered 8/4, 2020 at 14:57 Comment(1)
Works with Swift 5Edveh
A
9

Swift 3

The most upvoted answer is not correct for Swift 3.

enter image description here

The correct code to change color is:

self.navigationController?.navigationBar.tintColor = UIColor.white

If you want to change color, change UIColor.white above to the desired color

Assignor answered 17/2, 2017 at 22:0 Comment(1)
This was the only method that worked for me. Something seems broken with Apple's implementation right now...Loris
F
9

All the answers setting UINavigationBar.appearance().tintColor conflict with Apple's documentation in UIAppearance.h.

Note for iOS7: On iOS7 the tintColor property has moved to UIView, and now has special inherited behavior described in UIView.h. This inherited behavior can conflict with the appearance proxy, and therefore tintColor is now disallowed with the appearance proxy.

In Xcode, you need to command-click on each property you want to use with appearance proxy to inspect the header file and make sure the property is annotated with UI_APPEARANCE_SELECTOR.

So the correct way to color the navigation bar purple and the title and buttons white throughout the app via the appearance proxy is:

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().tintColor = .white

Note that UIBarButtonItem is not a subclass of UIView but rather NSObject. So its tintColor property is not the inherited tintColor from UIView.

Unfortunately, UIBarButtonItem.tintColor is not annotated with UI_APPEARANCE_SELECTOR – but that seems to me a documentation bug. The response from Apple Engineering in this radar states it is supported.

Forget answered 23/3, 2018 at 16:20 Comment(1)
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //alsoMourant
S
9

If you tried many times but could not work, you may try :

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red

Actually, I tried many times, only found this way will work.

Screenplay answered 13/12, 2020 at 13:9 Comment(4)
You are a life savor, no other solution worked for me, thanks mateMuddleheaded
Important to note that this code goes in the VC that is PREVIOUS to the one you are trying to change, not in the one you're trying to change.Pinard
Amazing! Thank you!!Waist
Worked like a charm, It worked for all view controller.Patrica
T
8

Swift 5 Updated

If you need to set Back button color globally, you could simply use:

UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color

Then you do not need to set back button background color on each view controller. If you use this one you COULD NOT SET BACK BUTTON COLOR ON ANOTHER VIEW CONTROLLER

BUT

If you need to set Back button color on view controller or change on another view controller do not use above method. You could use:

let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
                                  .foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
appearance.backgroundColor = .red // Navigation bar background color

self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance

navigationController?.navigationBar.tintColor = .green // Back button color
Toothwort answered 11/3, 2022 at 14:54 Comment(0)
S
7

Use this code in AppDelegate class, inside of didFinishLaunchingWithOptions.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UINavigationBar.appearance().tintColor = .white

}
Sung answered 20/4, 2017 at 9:31 Comment(0)
A
6
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

This snippet does the magic. Instead of the redColor, change it to as your wish.

Atrioventricular answered 26/2, 2015 at 5:55 Comment(0)
G
4

Lets try this code:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor.whiteColor()  // Back buttons and such
    navigationBarAppearace.barTintColor = UIColor.purpleColor()  // Bar's background color
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  // Title's text color

    self.window?.backgroundColor = UIColor.whiteColor()
    return true
}
Gastrology answered 8/4, 2016 at 5:38 Comment(1)
Thank you very much, sirHexapla
G
4

in swift 2.0 use

self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
Greiner answered 7/5, 2016 at 19:58 Comment(0)
J
4

Not sure why nobody has mentioned this...but I was doing exactly what you were doing in my viewDidLoad...and it wasn't working. Then I placed my code into viewWillAppear and it all worked.

The above solution is to change a single barbuttonItem. If you want to change the color for every navigationBar in your code then follow this answer.

Basically changing onto the class itself using appearance() is like making a global change on all instances of that view in your app. For more see here

Juror answered 16/5, 2017 at 0:11 Comment(0)
C
4

If you already have the back button in your "Settings" view controller and you want to change the back button color on the "Payment Information" view controller to something else, you can do it inside "Settings" view controller's prepare for segue like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "YourPaymentInformationSegue"
    {
        //Make the back button for "Payment Information" gray:
        self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray               
    }
}
Chainman answered 7/7, 2017 at 9:41 Comment(0)
V
2

Add following code to didFinishLaunchingWithOptions function in AppDelegate.swift

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade

// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
Vic answered 26/2, 2015 at 5:53 Comment(2)
This code gives me the following error: "Use of unresolved identifier 'uicolorFromHex'" Can anyone help me solve that?Hight
probably an extension for UIColor. You can search stackoverflow how to create an extension.Rodeo
L
2

For Swift 2.0, To change the Navigation-bar tint color, title text and back button tint color changed by using the following in AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  // Override point for customization after application launch.


    //Navigation bar tint color change

    UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)

    //Back button tint color change

    UINavigationBar.appearance().barStyle = UIBarStyle.Default
    UINavigationBar.appearance().tintColor =  UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)

    //Navigation Menu font tint color change

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent


    return true
}
Lamaism answered 24/2, 2016 at 5:50 Comment(0)
C
2

You have one choice hide your back button and make it with your self. Then set its color.

I did that:

self.navigationItem.setHidesBackButton(true, animated: true)
let backbtn = UIBarButtonItem(title: "Back", style:UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
self.navigationItem.leftBarButtonItem = backbtn
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.grayColor()
Clear answered 13/4, 2016 at 7:3 Comment(1)
The question was specifically around styling the left navigation item and this is the closest to answering the question directly.Feast
A
2
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

This works for me, iOS 9.0+

Albaugh answered 31/5, 2016 at 3:10 Comment(0)
B
2

For some reason it isn't working for me programmatically. However setting it in the storyboard works every time.

enter image description here

Buckjumper answered 4/5, 2022 at 4:55 Comment(0)
L
1

I prefer custom NavigationController rather than setting global ui, or put in ViewController.

Here is my solution


class AppNavigationController : UINavigationController {

  override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
  }

  override func viewWillAppear(_ animated: Bool) {

  }

}
extension AppNavigationController : UINavigationControllerDelegate {

  func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    let backButtonItem = UIBarButtonItem(
      title: "   ",
      style: UIBarButtonItem.Style.plain,
      target: nil,
      action: nil)
    backButtonItem.tintColor = UIColor.gray
    viewController.navigationItem.backBarButtonItem = backButtonItem
  }

  func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

  }

}

Also you don't need to mess with Apple Api like EKEventEditViewController,PickerViewController and so on if you use global settings ui like UIBarButtonItem.appearance().tintColor = .white

Latakia answered 5/6, 2019 at 13:0 Comment(0)
G
1

I'm using this in swift 5 and worked for me

navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
Gerrigerrie answered 17/2, 2021 at 8:6 Comment(0)
N
0

It will be solved with this line in -(void)viewDidLoad:

self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
Neibart answered 23/4, 2018 at 5:36 Comment(1)
(lldb) p self.navigationItem.backBarButtonItem (UIBarButtonItem *) $9 = nil (lldb) Whiffler
K
0

You should add this line

 self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
Karney answered 27/3, 2019 at 7:47 Comment(0)
K
0

Swift 5.3:

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
Krucik answered 24/9, 2020 at 18:16 Comment(0)
J
0

In SwiftUI, do the following:

Define a BackButton struct:

struct BackButton: View {
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        Button(action: {
            presentationMode.wrappedValue.dismiss()
        }) {
            HStack {
                Image(systemName: "chevron.left")
                    .foregroundColor(.white)
                Text("Back")
                    .foregroundColor(.white)
            }
        }
    }
}

In the second view, use it as follows:

struct SecondView: View {
    var body: some View {
        VStack {
            Text("Second View")
        }
        .navigationBarTitle("Second View", displayMode: .inline)
        .navigationBarBackButtonHidden(true)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                BackButton()
            }
        }
    }
}
Javelin answered 14/5, 2023 at 3:51 Comment(1)
This solution does work, but it's worth noting that using navigationBarBackButtonHidden(true) also disables the functionality of swiping to go back to the previous view.Airla

© 2022 - 2024 — McMap. All rights reserved.