user3766930
user3766930

Reputation: 5829

how can I set the tint color of right UIBarButtonItem in Swift?

I'm adding a right bar button to my navigation controller with the following code in viewDidLoad:

var b = UIBarButtonItem(
        image: UIImage(named: "settings"),
        style: .plain,
        target: self,
        action: #selector(sayHello(sender:))
    )

    self.navigationItem.rightBarButtonItem = b

my settings png file is white, but when I run the app, I see it in default blue color. Anyway, I want to change it to red. How can I do it?

I tried this:

let navigationBarAppearnce = UINavigationBar.appearance()
navigationBarAppearnce.tintColor = UIColor.red

but it didn't work. What's the proper way of doing that?

Upvotes: 0

Views: 3334

Answers (2)

AgentOverflow
AgentOverflow

Reputation: 33

In viewDidLoad(), after your button setup code, add:

navigationItem.rightBarButtonItem?.tintColor = UIColor.red

Upvotes: 0

Benjamin Lowry
Benjamin Lowry

Reputation: 3789

Have you tried:

let navigationController = UINavigationController() //not how you should actually get it, but just for purpose of example
navigationController.navigationBar.tintColor = UIColor.red

Edit:

I actually use the following:

navigationController.navigationBar.barTintColor = UIColor.red

Upvotes: 6

Related Questions