26

Basically I have a custom UIButton and this custom button contains subviews. If I add those subviews to my UIButton, then the button stops responding to event changes. I.e if I tap on it it doesn't respond to the selector. I have everything set as userInteractionEnabled. I also tried adding touchbegan and this is working. If I remove those subviews, the UIButton works again.

How Do I get the tap events from the button?

Monolo
  • 18,205
  • 17
  • 69
  • 103
user281300
  • 1,427
  • 2
  • 19
  • 31

1 Answers1

60

The subviews should have userInteractionEnabled set to NO. What is happening here is that the subviews are getting the touch events instead of the UIButton. If that doesn't work another option is to override hitTest:withEvent: in your custom UIButton so that it always returns itself and does not ask its subviews if they should handle the event. See the UIView docs for more details.

Jason Jenkins
  • 5,332
  • 3
  • 24
  • 29
  • Thanks a lot. You were right, i should not have userinteraction as enabled. – user281300 Apr 27 '10 at 22:11
  • I cannot tell how *how many times* this has borked me. I keep thinking it needs to be reversed. I'm gonna go play with an etch-a-sketch now. – Ben Kreeger Jan 25 '13 at 20:59
  • 1
    Thanks - for my subclassed button userInteractionEnabled didn't work but the hitTest approach did: - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { return self; } – earnshavian Sep 26 '13 at 20:13