Motivation gym5
Motivation gym5

Reputation: 291

Selected button color changes

Can anybody help me in changing the text color when user selected. Like I have three buttons on one viewcontroller and when users taps on 1 button it changes to white and the rest of two become grey.

Upvotes: 5

Views: 10979

Answers (2)

Shebin Koshy
Shebin Koshy

Reputation: 1192

You have to do same thing for all buttons in your interfaceBuilder

enter image description here

enter image description here

Then create outlet for all buttons

@IBOutlet weak var button1: UIButton!    
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!

Create action for all buttons

@IBAction func buttonOneAction(sender: AnyObject) {
    button1.selected = true;
    button2.selected = false;
    button3.selected = false;
}

@IBAction func buttonTwoAction(sender: AnyObject) {
    button1.selected = false;
    button2.selected = true;
    button3.selected = false;
}

@IBAction func buttonThreeAction(sender: AnyObject) {
    button1.selected = false;
    button2.selected = false;
    button3.selected = true;
}

output: middle button is selected

enter image description here

Upvotes: 9

blerch
blerch

Reputation: 395

You'll need to use setTitleColor(_:forState:).

// Setting the color for a button's disabled state to red

button.setTitleColor(UIColor.redColor(), forState: .Disabled)

UIButton Class Reference

Upvotes: 4

Related Questions