Highlight Selected Button

I have four buttons. I wanted to know how I would make it so that when one button is tapped by the user it highlights that button and when the user taps another button it highlights that button and unhighlights the other button. Right now when I tap the button it does nothing to highlight the button. This is the current code that I have:


extension UIButton {
    func setBackgroundColor(color: UIColor, forState: UIControl.State) {
        self.clipsToBounds = true  // add this to maintain corner radius
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let colorImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            self.setBackgroundImage(colorImage, for: forState)
        }
    }
}

override func viewDidLoad() {
        super.viewDidLoad()
        let defaults = UserDefaults.standard
        // Do any additional setup after loading the view, typically from a nib.
        button1Button.titleLabel?.adjustsFontSizeToFitWidth = true
        button2Button.titleLabel?.adjustsFontSizeToFitWidth = true
        button3.titleLabel?.adjustsFontSizeToFitWidth = true
        button4.titleLabel?.adjustsFontSizeToFitWidth = true
        button1.setBackgroundImage(UIImage(named: "button1"), for: .normal)
        button1.setTitleColor(UIColor.black, for: .normal)
       button2.setBackgroundImage(UIImage(named: "button2"), for: .normal)
        button2.setTitleColor(UIColor.black, for: .normal)
        button3.setBackgroundImage(UIImage(named: "button3"), for: .normal)
        button3.setTitleColor(UIColor.black, for: .normal)
        //Set background for pre-natal massage
        button4.setBackgroundImage(UIImage(named: "button4"), for: .normal)
        button4.setTitleColor(UIColor.black, for: .normal)
        selectedMassageType = 1
        buttonCatagory = .button2
        highlightButtonType()
        button1.titleLabel?.textAlignment = NSTextAlignment.center
        button2.titleLabel?.textAlignment = NSTextAlignment.center
        button3.titleLabel?.textAlignment = NSTextAlignment.center
        button4.titleLabel?.textAlignment = NSTextAlignment.center
    }
    
    func highlightButtonType() {
        
         button1.setBackgroundImage(UIImage(named: "button1"), for: .normal)
         button2.setBackgroundImage(UIImage(named: "button2"), for: .normal)
        button3.setBackgroundImage(UIImage(named: "button3"), for: .normal)
        button4.setBackgroundImage(UIImage(named: "button4"), for: .normal)
        switch selectedButtonType {
        case button1.tag :
            button1.setBackgroundColor(color: UIColor.customWhite, forState: .highlighted)
        case button2.tag :
            button2.backgroundColor = UIColor.customBlue
        case button3.tag :
            button3.backgroundColor = UIColor.customBlue
        case button4.tag:
            button4.backgroundColor = UIColor.customBlue
        default: break
        }
    }
Answered by Claude31 in 348045022

You should add a layer to the buttons, and set the opacity as needed.

Where do you call highlightButtonType ?


Where is selectedButtonType set ?


From what we see here, buttons 2 to 4 are always set to customBlue, button 1 to customWhite. So, it cannot change.

I call the highlightButtonType in the view did load. What do mean by Where is selectedButtonType set? As well as to how do I get it so that when I tap the button it changes to the color that I want.

You call highlight in viewDidLoad. But where does it change ? viewDidLoad is only called once.


Similarly, where is selectedButtonType modified after tap on a button ? It is set to 1 in viewDidLoad, but nowhere changed in the code you posted.

I gave you all the code that I have the only thing I forgot to put in was this:


enum ButtonType: Int{
    case button1 = 1
    case button2 = 2
    case button3 = 3
    case button4 = 4
    func description() -> String{
        switch self{
        case .button1: return "button1"
        case .button2: return "button2"
        case .button3: return "button3"
        case .button4: return "button4"
        }
    }
    
}

I have this code outside the class.

Accepted Answer

You should add a layer to the buttons, and set the opacity as needed.

Highlight Selected Button
 
 
Q