Search code examples
iosswiftuibarbuttonitemuitoolbar

Change UIBarButtonItem color when selected


I'm using images for UIBarButtonItem which are pure white and transparent. How can I change the tint color when the user selects an item? Is there any method which tells me what button is selected inside a UIToolBar?


Solution

  • You can easily change the tintColor of your UIBarButtonItem when the user taps it like so:

    @IBAction func myToolBarButton(sender: AnyObject) {
        myToolBarButton.tintColor = UIColor.greenColor()
    }
    

    You may want to use a UITabBar instead if the user can only select one button at a time. A UITabBar will change the tintColor of the button that is selected and then change the button back to its default color when you select another button.

    If you keep the UIToolbar you will have to handle the color changes yourself.