Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of disabled bar button item in iOS

Tags:

xcode

ios

swift

I need to display my app's icon in the navigation bar. To do this, I have added it as a right bar button item. I don't want it to be clickable, I just need the icon there, so I set it to disabled. The problem with this is the icon appears grey, instead of green. Is there a way to disable this button but also keep it's original color?

like image 479
Laura M Avatar asked Mar 02 '16 01:03

Laura M


1 Answers

Try this:

let barButtonItem = UIBarButtonItem(title: "Click", style: .Done, target: self, action: #selector(didClick(_:)))
barButtonItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Normal)
barButtonItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Disabled)
barButtonItem.enabled = false
navigationItem.setRightBarButtonItem(barButtonItem, animated: false)
like image 96
J.Wang Avatar answered Nov 15 '22 07:11

J.Wang