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条回答
男人必须洒脱
2楼-- · 2019-01-16 05:40

Very easy to set up in the storyboard:

enter image description here

enter image description here

查看更多
唯我独甜
3楼-- · 2019-01-16 05:40

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

self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
查看更多
【Aperson】
4楼-- · 2019-01-16 05:43

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
    }
查看更多
别忘想泡老子
5楼-- · 2019-01-16 05:45

You should use this:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
查看更多
男人必须洒脱
6楼-- · 2019-01-16 05:48

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

查看更多
Ridiculous、
7楼-- · 2019-01-16 05:48
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

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

查看更多
登录 后发表回答