Mohammed Nabil
Mohammed Nabil

Reputation: 157

Button Color Changes when selection

For example, I have two buttons (button 1 & button 2).

If I click on button 2, then background color will be red and text color white.

If I click on button 1, it will become grey background color and red text color.

And if I click on button 1 then background color will be red and text color white, and button 2 will become grey background color and red text color.

So how to do this programmatically?

@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button1: UIButton!
    @IBAction func btnbutton2(_ sender: Any) {
        if (buttonTest2.isSelected == false){
        button2.backgroundColor = UIColor.gray
            button2.setTitleColor(UIColor.red, for: .normal)}
        if (button2.isSelected == true){
            button2.backgroundColor = UIColor.systemRed
            button2.setTitleColor(UIColor.white, for: .normal)}
    }
    @IBAction func btnbutton1(_ sender: Any) {
        if (button1.isSelected == true){
            button1.backgroundColor = UIColor.systemRed
            button1.setTitleColor(UIColor.white, for: .normal)}
        if (button1.isSelected == false){
        button1.backgroundColor = UIColor.gray
            button1.setTitleColor(UIColor.red, for: .normal)}
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        button1.backgroundColor = UIColor.systemRed
        button1.setTitleColor(UIColor.white, for: .normal)
        button2.backgroundColor = UIColor.gray
        button2.setTitleColor(UIColor.red, for: .normal)}

Upvotes: 1

Views: 873

Answers (1)

DonMag
DonMag

Reputation: 77486

The Selected property of a UIButton only affects the title color, if a color has been set for the Selected state.

You can set the title color for .normal .highlighted and .selected -- there is no corresponding states for the background color.

In addition, tapping a button does not change its Selected state... you have to do that yourself.

Try it like this:

class SelectButtonViewController: UIViewController {
    
    @IBOutlet var button1: UIButton!
    @IBOutlet var button2: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        button1.isSelected = true

        // start button1 with "selected" state properties
        button1.backgroundColor = UIColor.systemRed
        button1.setTitleColor(UIColor.red, for: .normal)
        button1.setTitleColor(UIColor.lightGray, for: .highlighted)
        button1.setTitleColor(UIColor.white, for: .selected)

        // start button1 with "not selected" state properties
        button2.backgroundColor = UIColor.gray
        button2.setTitleColor(UIColor.red, for: .normal)
        button2.setTitleColor(UIColor.lightGray, for: .highlighted)
        button2.setTitleColor(UIColor.white, for: .selected)

    }
    
    @IBAction func btnbutton1(_ sender: Any) {
        button1.isSelected = true
        button1.backgroundColor = .systemRed
        button2.isSelected = false
        button2.backgroundColor = .gray
    }
    @IBAction func btnbutton2(_ sender: Any) {
        button2.isSelected = true
        button2.backgroundColor = .systemRed
        button1.isSelected = false
        button1.backgroundColor = .gray
    }

}

Upvotes: 1

Related Questions