自定义 UIButton + 子视图 = 无事件

Custom UIButton + subviews = no events(自定义 UIButton + 子视图 = 无事件)
本文介绍了自定义 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 + 子视图 = 无事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)