0

I have a UIButton not responding to touch event. And the button only has some subviews not handling events. So I replaced it with my customized UIButton to find out what happened. Below is my code:

@interface CustomButton : UIButton

@end

@implementation CustomButton

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
}

-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    return [super beginTrackingWithTouch:touch withEvent:event];
}
@end

I found out that the touchesBegan:withEvent: method was called, but the beginTrackingWithTouch:withEvent: was never called. It seems that the event has been dispatched to my button, but I don't understand why the event has never been tracked. Any clue will be appreciated.

Neal.Marlin
  • 494
  • 5
  • 17

1 Answers1

1

Disable user interaction of views that are above the button which are not listening for events. Check that the button has user interaction enabled. Check the frame size of UIButton and ensure that it is not zero specifically when using auto layout programatically.

John Doe
  • 2,225
  • 6
  • 16
  • 44
  • Disable user interaction of views that are above the button fix my problem. But one thing confused me is that the subview is forwarding touch event to button, but why it is not recognized? I experimented the hitTest method, and find out that if you disable the above view, the hitTest view will be the button, if not it will be the above view. It seems to be the difference. Even though the hitTest view is not the button, as the default behavior of UIView and responder chain, the event will be forwarded to the button properly. touch methods of the button was called may prove all this. – Neal.Marlin Jul 05 '19 at 00:03
  • In a word, my problem is why the UIButton doesn’t track touch event forwarded to it by its subview? It is designated to be like this, or a bug? – Neal.Marlin Jul 05 '19 at 00:10