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.
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.