Reputation: 291
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
Reputation: 1192
You have to do same thing for all buttons in your interfaceBuilder
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
Upvotes: 9
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)
Upvotes: 4