I want to make a UIButton so that when its tapped the button becomes highlighted. However, I don't know what code to write for this. What I have done so far is just make a new swift file.
I think the best way to do it is to use segmented control.
- Declare a var in the viewController to track selected button
var selectedButton = 0
- Set tags 1, 2, 3 for your buttons, in IB
- You could create an array to hold the buttons, I will not show here, for simplicity sake
- You will probably set this var when you prepare to segue to the view
- Write a func to handle the hilite : hilite the selected, unhilite the others
func hiliteButtons() {
if button1.tag == selectedButton {
button1.backgroundColor = .red
} else {
button1.backgroundColor = .clear
}
// Repeat for the 2 other buttons
}
Call this func in each IBAction after setting selectedButton to sender.tag