[英]UIButton tap not responding
I added a new UIButton 我添加了一个新的UIButton
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setFrame:CGRectMake(x, y, width, height)];
[newButton setTitle:@"" forState:UIControlStateNormal];
[newButton setTag:x+INDEX_OFFSET];
[newButton setBackgroundImage:image forState:UIControlStateNormal];
[newButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:newButton];
[self bringSubviewToFront:newButton];
and I can see it on the screen but it is not responding to a tap gesture. 我可以在屏幕上看到它,但它没有响应轻击手势。 It should call "buttonPressed".
它应该调用“buttonPressed”。 Any help would be super!
任何帮助都会超级!
One of the possible problem is that you are adding the button in a frame position out its superview. 可能的问题之一是您在超视图的框架位置添加按钮。 Just to check it easily try to set
clipToBounds
property to YES in its superview bounds, then run the app. 只是为了检查它,尝试在其超视图边界中将
clipToBounds
属性设置为YES,然后运行应用程序。 If you don't see the button it means that you set the button position out superivew position, that's why it doesn't respond to touches. 如果您没有看到按钮,则意味着您将按钮位置设置为超出位置,这就是它不响应触摸的原因。
Another possible problem is if you add the UIButton
as a subview
to an UIImageView
(which you shouldn't, but it works) . 另一个可能的问题是,如果您将
UIButton
作为subview
添加到UIImageView
(您不应该,但它可以工作) 。 In this case, you could either add it to a UIView
or set UIImageView
's userInteractionEnabled
property to YES
. 在这种情况下,您可以将其添加到
UIView
或将UIImageView
的userInteractionEnabled
属性设置为YES
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.