Change color of Back button in navigation bar

2019-01-16 05:21发布

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?

22条回答
可以哭但决不认输i
2楼-- · 2019-01-16 05:32

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
查看更多
该账号已被封号
3楼-- · 2019-01-16 05:32

In Swift3, To set the Back button to red.

self.navigationController?.navigationBar.tintColor = UIColor.red
查看更多
再贱就再见
4楼-- · 2019-01-16 05:33

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()]
查看更多
太酷不给撩
5楼-- · 2019-01-16 05:34

Use this code in AppDelegate class, inside of didFinishLaunchingWithOptions.

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

UINavigationBar.appearance().tintColor = .white

}
查看更多
劫难
6楼-- · 2019-01-16 05:34

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
}
查看更多
Anthone
7楼-- · 2019-01-16 05:39

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

查看更多
登录 后发表回答