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 + 子视图 = 无事件
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01