Custom UIButton + subviews = no events(自定义 UIButton + 子视图 = 无事件)
问题描述
基本上我有一个自定义 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
设置为 NO
.这里发生的是子视图正在获取触摸事件而不是 UIButton.如果这不起作用,另一种选择是在您的自定义 UIButton 中覆盖 hitTest:withEvent:
,以便它始终返回自身并且不会询问其子视图是否应该处理该事件.请参阅 UIView 文档 了解更多详情.
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 + 子视图 = 无事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义 UIButton + 子视图 = 无事件
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01