2

I have a UIBarButtonItem defined as follows:

let cardNavButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "card")!, style: UIBarButtonItemStyle.Bordered, target: self, action: self.headerCardButtonSelector)
self.navigationItem.rightBarButtonItem = cardNavButton
cardNavButton.tintColor = UIColor.whiteColor()

When the user tapped the button its color changes. How can I define the color which is displayed when the user tapps the button?

The image which is displayed in this button has more than one color but it is displayed in only one color. How can I change that?

Michael
  • 32,527
  • 49
  • 210
  • 370

2 Answers2

0

You can create UIBarButtonItem with your custom UIButton and set properly colors for states.

            let customButton: UIButton = UIButton()
            //customize yout custom button
            let cardNavButton: UIBarButtonItem = UIBarButtonItem(customView: customButton)
            self.navigationItem.rightBarButtonItem = cardNavButton
  • How do I set properly colors for states? – Michael Jun 17 '15 at 14:26
  • You can use this methode for change title color [titleColorForState](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/titleColorForState:) for change background color you can use setBackgroundImageForState or override highlite methode in UIButton [stackoverflow answer](http://stackoverflow.com/questions/14523348/how-to-change-the-background-color-of-a-uibutton-while-its-highlighted) – Aleksei Belezeko Jun 17 '15 at 15:22
0

Supposing "s" is a proper hex value string such as green "00ff00", use this code when you tap the button:

if let num2 = Int(s, radix: 16) {
                flamingoBtn.tintColor = UIColor(netHex:num2)
            }
            else{
                flamingoBtn.tintColor = UIColor.blueColor() //fallback to some default color
            }
Josh
  • 6,251
  • 2
  • 46
  • 73