Button Properties - How to Change when Tapped

Hi!

I need a function that changes the background colour of the button that is tapped, on a UI with four different buttons.

Please help! Thanks.
You can override the isHilited method: https://stackoverflow.com/questions/14523348/how-to-change-the-background-color-of-a-uibutton-while-its-highlighted

You can also define background image (if the button is not rounded) with a plain color and change the background image.
for the isHighlighted method, would someone be able to give some example code with everything declared? thanks!
The SO thread gives a lot of code, isn't it enough ?

For instance, this extension to set a color image.
Code Block
extension UIButton {
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
self.setBackgroundImage(imageWithColor(color), forState: state)
}
}


For isHilighted, declare a subclass of UIButton

Code Block
class MyButton: UIButton {
override open var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? UIColor.blue : UIColor.red
}
}
}

Button Properties - How to Change when Tapped
 
 
Q