问题
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?
回答1:
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.
回答2:
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 =
回答3:
Very easy to set up in the storyboard:
回答4:
You should use this:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
回答5:
Swift
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
}
回答6:
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
}
回答7:
Swift 4.2
Change complete app theme
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
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)
回答8:
In Swift3, To set the Back button to red
.
self.navigationController?.navigationBar.tintColor = UIColor.red
回答9:
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
回答10:
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
This snippet does the magic. Instead of the redColor, change it to as your wish.
回答11:
Swift 3
The most upvoted answer is not correct for Swift 3.
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
回答12:
Use this code in AppDelegate
class, inside of didFinishLaunchingWithOptions
.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
回答13:
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.
回答14:
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
}
回答15:
in swift 2.0 use
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
回答16:
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
}
}
回答17:
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()]
回答18:
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
}
回答19:
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
This works for me, iOS 9.0+
回答20:
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
回答21:
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
回答22:
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()
回答23:
It will be solved with this line in -(void)viewDidLoad:
self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
回答24:
You should add this line
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
来源:https://stackoverflow.com/questions/28733936/change-color-of-back-button-in-navigation-bar