自定义UIButton + subviews =无事件 [英] Custom UIButton + subviews = no events
问题描述
基本上我有一个自定义UIButton,这个自定义按钮包含子视图。如果我将这些子视图添加到我的UIButton,则该按钮会停止响应事件更改。即如果我点击它它不响应选择器。我将所有内容设置为 userInteractionEnabled
。我也试过添加touchbegan,这是有效的。如果删除这些子视图,UIButton将再次运行。
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?
推荐答案
子视图应该 userInteractionEnabled
设置为否
。这里发生的是子视图获取触摸事件而不是UIButton。如果这不起作用,另一个选项是在您的自定义UIButton中覆盖 hitTest:withEvent:
,以便它始终返回自身并且不询问其子视图是否应该处理事件。请参阅 UIView docs 了解更多详情。
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.
这篇关于自定义UIButton + subviews =无事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!