Changing color of button text and state

To change color of text

button.titleLabel.textColor = UIColor.grayColor()

To change state, on button press add following -

button.enabled = true

IBAction method should be like -

@IBAction func buttonTapped(sender : UIButton!) {
    sender.enabled = false
}

In swift you change color for a specific State with the setTitleColor method.

In you case it will be :

button.setTitleColor(UIColor.grayColor, forState: UIControlState.Normal)

Swift 5 Update:

button.setTitleColor(UIColor.grayColor, for: UIControl.State.normal)

Swift 3

button.setTitleColor(UIColor.gray, for: UIControlState.normal)

Note that;

  • grayColor has been renamed to gray
  • Normal is now normal (lowercase)

You have to set the text colour for the specific button state.

Tags:

Swift