0

I'm trying to change the backgroundColor for a button on tap. When the button is tapped, it should change the backgroundColor. But my problem is that I need two taps for this event. And I don't understand why.

My code:

for (index, cue) in cuestionesCC.enumerated(){
    let buttonYes = UIButton()
    buttonYes.backgroundColor = .green
    buttonYes.titleEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10) // Add padding around text
    buttonYes.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    buttonYes.tag = 1
    buttonYes.setTitle("Yes", for: .normal)
    buttonYes.addTarget(self, action: #selector(buttonActionYesNo), for: .touchUpInside)

    let buttonNo = UIButton()
    buttonNo.backgroundColor = .red
    buttonNo.titleEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10) // Add padding around text
    buttonNo.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    buttonNo.tag = 2
    buttonNo.setTitle("No", for: .normal)
    buttonNo.addTarget(self, action: #selector(buttonActionYesNo), for: .touchUpInside)

    stackViewH.addArrangedSubview(buttonYes)
    stackViewH.addArrangedSubview(buttonNo)
}

@objc func buttonActionYesNo(sender: UIButton!) {
    print (sender.tag)
    sender.backgroundColor = .gray
}

When I tap the button once, it prints the tag. But I need another tap additional for change the backgroundColor of the button. I don't understand that logic.

koen
  • 5,383
  • 7
  • 50
  • 89
Mimmetico
  • 422
  • 9
  • 25
  • The problem might be that you don't get a redraw because the button is busy animating the tap. Try adding a `delay` around `sender.backgroundColor = .gray`. Start with a big delay, like 1 second, and see if that helps. If it does, try shorter delays. – matt Oct 17 '19 at 16:00
  • Maybe try for .touchDown rather than for .touchUpInside and see if that makes a difference? – rs7 Oct 17 '19 at 16:22
  • Yes, I tried .touchDown, .touchOutInside, ... all events... but nothing happens. The strange thing is that it does read the print at the first touch, but the color change only at the second touch. – Mimmetico Oct 17 '19 at 16:29
  • I've also tried the delay in a new thread, but it doesn't seem to work either. – Mimmetico Oct 17 '19 at 16:30
  • Possible duplicate of [How to change the background color of a UIButton while it's highlighted?](https://stackoverflow.com/questions/14523348/how-to-change-the-background-color-of-a-uibutton-while-its-highlighted) – koen Oct 17 '19 at 16:35
  • I have reviewed that thread, but it is not relevant to my question. – Mimmetico Oct 17 '19 at 16:50
  • I'm not able to reproduce the same. Do you have your code hosted somewhere? Github may be? – Subramanian Mariappan Oct 17 '19 at 16:52
  • What is `cuestionesCC`? Maybe your button gets overwritten by a new one in the for loop after you tap and therefore you don't see the color change. – koen Oct 17 '19 at 16:56
  • cuestionesCC is a Array. Its value is dynamic and may vary. Sometimes it has 5 elements, other 10, other 8... the program goes through that array and creates as many pairs of YES / NO buttons as the array indicates. This works well. Create a list with several questions and each question two buttons: Yes and No. – Mimmetico Oct 17 '19 at 17:00
  • Did you see this: https://stackoverflow.com/questions/13750014/ios-double-tap-on-uibutton ? – koen Oct 17 '19 at 17:26
  • Yes, i saw and tried to did it with UITapGestureRecognizer, but don't work. (i hate swift and its ilogic logic) – Mimmetico Oct 18 '19 at 10:55

0 Answers0