1

I have a custom UIButton but I am not able to make changing background or text color based on a quick tap. If something works, it's only on long press:

    buton.isUserInteractionEnabled = true
    buton.setTitle("text_normal", for: .normal)
    buton.setTitle("text_highlighted", for: .highlighted)
    buton.setTitle("text_selected", for: .selected)
    buton.setTitle("text_focused", for: .focused)

The only text I can get is "text_highlighted" after holding the button ~1 second. But nothing happens on short tap.

But the action is triggered correctly:

let tap2 = UITapGestureRecognizer(target: self, action: #selector(handleTap))
buton.addGestureRecognizer(tap2)


@objc func handleTap(sender: UITapGestureRecognizer) {
    print("click")
}

What I tried: Adding custom

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
...

which is also triggered later. And combining with tocuhesEnded didn't change the color temporarily as well (maybe it was too quick).

I can't use UIButton(type: .system) as well - it's not system styled button.

Similar as with changing text this is not working as well:

    self.setTitleColor(UIColor.gray, for: .normal)
    self.setTitleColor(UIColor.yellow, for: .selected)
    self.setTitleColor(UIColor.yellow, for: .highlighted)
    self.setTitleColor(UIColor.yellow, for: .focused)

I can see yellow only when long pressed. I am looking for something like this, but it needs to work on quick tap as well.

Custom button is modifying layoutSubviews(), but not colors. Custom button contains default image and label. Whole button has rounded corners. But overall nothing special is in there.

I am not using any storyboard or XIB - everything is in Swift 4 programatically. The button is supposed to lead to another ViewController, but I want to see the immediate feedback on click. Something like when created from storyboard: https://youtu.be/lksW12megQg?t=3m25s - not even simple alpha change works for me right now.

Tunerx
  • 619
  • 9
  • 21
  • Why are you adding a `UITapGestureRecognizer`, buttons job is to detect taps, so don't override anything but use what you have above with setting titles and title colors for different states and `UIButton` will do the rest – Ladislav Jul 30 '18 at 10:03
  • After removing `UITapGestureRecognizer` completely it's still the same - short click on the button is not modifying the text or the color of it. Only after longer press as before. There is no other gesture control now on that button. – Tunerx Jul 30 '18 at 10:18
  • Are you useing `.custom` type button? – Ladislav Jul 30 '18 at 10:24
  • Just plain vanila UIButton with settings like `btn.setTitle("Ladi", for: .normal)` and `btn.setTitle("Slav", for: .highlighted)` will work...So you must be doing something inside your `UIButton` subclass to make it not work... – Ladislav Jul 30 '18 at 10:28
  • This is my code now: `let buton = UIButton() buton.setTitle("text1", for: .normal) buton.setTitle("text2", for: .highlighted) container.addSubview(buton)` But it's the same - short tap does nothing, long press does this to change to text2. The only thing I can see is that my main view (container) is scrollview. – Tunerx Jul 30 '18 at 10:35
  • Show in what method you are adding these? – Ladislav Jul 30 '18 at 10:36
  • in viewDidLoad() of a custom ViewController. Now I added regular target `buton.addTarget(self, action: #selector(handleTap(_:)), for: .touchUpInside)` which is fine (I have a log after each tap) but nothing is happening with the UIButton visually. No alpha, no color change (when I added it for this simple button), no text change. And to be more precise - it's not really a second, it's a bit less - but still I have to press it longer. Maybe the problem is that the 'ease in' and 'ease out' is not happening (as with default alpha) but it swaps quickly, so it's not even drawn or something. – Tunerx Jul 30 '18 at 10:48
  • Now I tried to have the button just like this: `let buton = UIButton(type: .system) buton.setTitle("text1", for: .normal)` - just waiting for the alpha to happen in system styled button. And the behaviour is still the same - tap does nothing, longer press again nicely shows the alpha (and after releasing slowly goes back from alpha to normal). – Tunerx Jul 30 '18 at 10:57
  • If you want self.setTitleColor(UIColor.yellow, for: .selected) & buton.setTitle("text_selected", for: .selected) to work as expected then the state of the button should be selected. Create an IBAction on button .touchUpInside and add code button.isSelected = true. – Jen Jose Jul 30 '18 at 13:50

2 Answers2

0

check isselected property of uibutton if button is selected then change the background color of button

Protocol
  • 1,696
  • 13
  • 30
  • this would change the colour permanently - I want to only show the tap happened (as default UIButton presents it via temporarily changed alpha - which is also not working for me) – Tunerx Jul 30 '18 at 11:23
0

Jen Jose was right - there was a problem with my parent class which was 'eating up' my touches. I confirmed this when moving it to different ViewController + I had the same issue with table, which couldn't handle touch events (https://stackoverflow.com/a/9248827/1317362)

EDIT:

To be precise - this button was in a UIScrollView.

Adding scrollView.delaysContentTouches = NO; solved the issue completely.

Details: https://stackoverflow.com/a/16650610/1317362

Tunerx
  • 619
  • 9
  • 21