Change color of Back button in navigation bar
IosSwiftUinavigationbarUibarbuttonitemBackbarbuttonitemIos Problem Overview
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:
How do I make that button white?
Ios Solutions
Solution 1 - Ios
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.
Solution 2 - Ios
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 =
Solution 3 - Ios
Solution 4 - Ios
You should use this:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
Solution 5 - Ios
Swift
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
}
Solution 6 - Ios
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)
Solution 7 - Ios
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
}
Solution 8 - Ios
In Swift3, To set the Back button to red
.
self.navigationController?.navigationBar.tintColor = UIColor.red
Solution 9 - Ios
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
Solution 10 - Ios
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
Solution 11 - Ios
Swift 3
The most upvoted answer is not correct for Swift 3.
[![enter image description here][1]][1]
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 [1]: https://i.stack.imgur.com/GJN88.png
Solution 12 - Ios
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.
Solution 13 - Ios
Use this code in AppDelegate
class, inside of didFinishLaunchingWithOptions
.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
Solution 14 - Ios
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
This snippet does the magic. Instead of the redColor, change it to as your wish.
Solution 15 - Ios
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
}
Solution 16 - Ios
in swift 2.0 use
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
Solution 17 - Ios
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
}
}
Solution 18 - Ios
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.
Solution 19 - Ios
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
This works for me, iOS 9.0+
Solution 20 - Ios
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
Solution 21 - Ios
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()]
Solution 22 - Ios
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
}
Solution 23 - Ios
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()
Solution 24 - Ios
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
Solution 25 - Ios
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
Solution 26 - Ios
I'm using this in swift 5 and worked for me
navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
Solution 27 - Ios
It will be solved with this line in -(void)viewDidLoad:
self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
Solution 28 - Ios
You should add this line
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
Solution 29 - Ios
Swift 5.3:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")