简体   繁体   English

UIButton不响应触摸事件

[英]UIButton do not respond to touch event

I have a UIButton not responding to touch event. 我有一个UIButton不响应触摸事件。 And the button only has some subviews not handling events. 而且该按钮只有一些子视图,无法处理事件。 So I replaced it with my customized UIButton to find out what happened. 因此,我用自定义的UIButton替换了它,以了解发生了什么。 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. 我发现touchesBegan:withEvent:方法被调用了,但是从未调用beginTrackingWithTouch:withEvent: 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. 检查UIButton的框架大小,并确保它不为零(特别是在以编程方式使用自动布局时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM