Highlight Button

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.

Accepted Reply

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

Replies

Please show your code including the `UIButton`.


You would get a more appropriate answer sooner, with enough code shown.


And you should better describe what you want.


- What sort of style do you want for your `hilighted` state?

- The button gets `hilighted` while user is pressing the button? Or on each tap, the `hilighted` state flips from on to off and vice versa?

Highlighting (flashing) is automatic when you tap. There is no code to write.


So, what is your goal ?

- Do you want to make highlighting persistent ?

- What highlight effect do you want ?


You can change color or background


- Create an IBOutlet for the button.


- Then, you can change the text color to hilite with:

        buttonToHilite.setTitleColor(.red, for: .normal)

What I want to do is right now I have 3 buttons. I want one of the 3 buttons to be preselected, then if someone, where to tap one of the other buttons the highlight, would switch to that button. The button would also stay highlighted.

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

I have a quick question that’s completely off topic. I recently learned Swift and I can read the code but I have trouble writing it is that normal.

Yes, that the same with a foreign language ! You start understanding what you rad or hear, but it is more difficult to speak your own sentences.


This is why it is important to type code oneself and not just copy and paste code. And to start with simple constructions.


Good luck. And don’t forget to close the thread by marking the correct answer.

I wanted to know how to set tags in Swift.

Many objects (like buttions, textFields, …) ahev a tag property.


just write

myButton.tag = 1


Or you can also set the tag "statically" in IB.

This is the code that I have written so far, and it's not working. I want the highlight to switch between buttons. For example, if I select therapistMale I want to highlight but if I change my mind, and decide to go with eitherTherapist I want the highlight on the male therapist to go away and highlight eitherTherapist button.


func hiliteButtons() {

if therapistGenderMale.tag == selectedButton {

therapistGenderMale.backgroundColor = .lightGray

} else if eitherGender.tag == selectedButton{

eitherGender.backgroundColor = .lightGray

therapistGenderMale.backgroundColor = .white

}else if therapistGenderFemale.tag == selectedButton{

therapistGenderFemale.backgroundColor = .lightGray

eitherGender.backgroundColor = .white

}else if therapistGenderFemale.tag != selectedButton{

therapistGenderFemale.backgroundColor = .white

}

}

Can you test the values ? And tell what you get ?

func hiliteButtons() {

     print("selectedButton", selectedButton)
     print("therapistGenderMale.tag", therapistGenderMale.tag)     // Idem for all others

        if therapistGenderMale.tag == selectedButton {
            therapistGenderMale.backgroundColor = .lightGray
        } else if eitherGender.tag == selectedButton{
            eitherGender.backgroundColor = .lightGray
            therapistGenderMale.backgroundColor = .white
        }else if therapistGenderFemale.tag == selectedButton{
            therapistGenderFemale.backgroundColor = .lightGray
            eitherGender.backgroundColor = .white
        }else if therapistGenderFemale.tag != selectedButton{
            therapistGenderFemale.backgroundColor = .white
        }
    }


I do not understand the last test

You can write a more readable code like this:


func hiliteButtons() {

      therapistGenderMale.backgroundColor = .white     // By default, each is white
      eitherGender.backgroundColor = .white
     therapistGenderFemale.backgroundColor = .white
     switch selectedButton {
        case therapistGenderMale.tag :
            therapistGenderMale.backgroundColor = .lightGray
        case eitherGender.tag :
            eitherGender.backgroundColor = .lightGray
       case therapistGenderFemale.tag :
            therapistGenderFemale.backgroundColor = .lightGray
       default: break
        }
  }

I implemented this code, and I attached a picture of what happens. The app crashes. and then you cant press anything else.

  1. var selectedButton = 0
  2. func hiliteButtons() {
  3. therapistGenderMale.backgroundColor = .white // By default, each is white
  4. eitherGender.backgroundColor = .white
  5. therapistGenderFemale.backgroundColor = .white
  6. switch selectedButton {
  7. case therapistGenderMale.tag :
  8. therapistGenderMale.backgroundColor = .lightGray
  9. case eitherGender.tag :
  10. eitherGender.backgroundColor = .lightGray
  11. case therapistGenderFemale.tag :
  12. therapistGenderFemale.backgroundColor = .lightGray
  13. default: break
  14. }
  15. }



Did you test


print("selectedButton", selectedButton)

print("therapistGenderMale.tag", therapistGenderMale.tag) // Idem for all others


Please, do it, it is difficult to help if you do not provide information on the problem.


Which is line is the crash ?


What is the crash log ?

I wanted to know how to put Buttons in an Array. As well as what did you mean when you said you will probably set this var when you prepare to segue to the view, and how do you do that.

You did not answer my 2 questions


What is the result of

print("selectedButton", selectedButton)

print("therapistGenderMale.tag", therapistGenderMale.tag) // Idem for all others


Where exactly is the error, and what error message ?


To create an array:

- create a button in IB

- control-drag to viewController, and select Outlet collection

- you get this declaration:

@IBOutlet var allButtons: [UIButton]!

- create a second button in IB

- control-drag from the IBOutlet declaration to this new button, it will be included in Outlet collection ([UIButton]


What I mean

- when you come from a view to the ViewController with allButtons,

- in prepare, you write

destVC.selectedButton = thePreselectedButton

Once you press any of the buttons the app just stops working and you cant press any buttons. It tells me thread 1 sigabrt. I still don't understand how to make an array and still don't understand what you mean when you say you will probably set this var when you prepare to segue to the view.