Change Button Properties when Tapped?


I have a UI with multiple buttons, and I need the button that is tapped to change its background colour to red. Is there a way to do this as a function, something like (but obviously not):

buttonTapped.backgroundColor =

Please help!


Hi Tom,

Welcome to the Code Crew community.

If you have the buttons grouped such that when any one of them are tapped they call the same IBAction code then the way to discriminate between the different buttons is to give each one a different tag value in storyboard. Make sure that your IBAction sender: parameter is defined as UIButton rather than Any.

Then in your IBAction code you can set sender.backgroundColor = or you can use a switch statement on sender.tag and for a given tag value set a different color.