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.leftBarButton
In Swift3, To set the Back button to red
.
self.navigationController?.navigationBar.tintColor = UIColor.red
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
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
}
Swift 5.3:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
This snippet does the magic. Instead of the redColor, change it to as your wish.
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 toUIView
, and now has special inherited behavior described inUIView.h
. This inherited behavior can conflict with the appearance proxy, and thereforetintColor
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.