尽管 userInteractionEnabled = true,但 UIView 中的 UIButton 没有响应触摸事件
Posted
技术标签:
【中文标题】尽管 userInteractionEnabled = true,但 UIView 中的 UIButton 没有响应触摸事件【英文标题】:UIButton in UIView not responding to touch events despite userInteractionEnabled = true 【发布时间】:2015-07-30 21:44:42 【问题描述】:我很难解决这个问题。我有一个自定义 UIView,它包含一个 UIImageView 和一个 UIButton。自定义的 UIView 称为 PPButton,添加这些控件的代码如下。需要注意的一件事是我正在覆盖 drawRect,这不应该影响这里发生的事情。
private func addSubviews()
let buttonFontMultipllier : CGFloat = 4.5 / 10.0
let buttonFontSize = (self.subRect!.height) * buttonFontMultipllier
self.ppButton = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton
self.ppButton!.setTitle(self.ppButtonTitle!, forState: .Normal)
self.ppButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.ppButton!.titleLabel!.font = UIFont(name: "Formata-BoldCondensed", size: buttonFontSize)
self.ppButton!.contentHorizontalAlignment = .Left
self.ppButton!.addTarget(self, action:"buttonWasTapped:", forControlEvents: UIControlEvents.TouchUpInside)
self.ppButton!.userInteractionEnabled = true
self.ppButton!.backgroundColor = UIColor.greenColor()
self.ppButton!.layer.borderColor = UIColor.greenColor().CGColor
self.ppButton!.layer.borderWidth = 1.0
self.ppImage = UIImageView()
self.ppImage!.contentMode = UIViewContentMode.ScaleAspectFit
self.ppImage!.userInteractionEnabled = false
self.ppImage!.layer.borderColor = UIColor.greenColor().CGColor
self.ppImage!.layer.borderWidth = 1.0
self.ppButtonView!.addSubview(self.ppButton!)
self.ppButtonView!.addSubview(self.ppImage!)
self.ppButtonView!.bringSubviewToFront(self.ppButton!)
self.ppButtonView!.sendSubviewToBack(self.ppImage!)
self.ppButtonView!.userInteractionEnabled = false;
self.addSubview(self.ppButtonView!)
superview!.layer.backgroundColor = UIColor.yellowColor().CGColor
这些按钮被添加到另一个自定义视图中,该视图被添加到自定义视图控制器中。将 PPButtons 添加到自定义 HomeView 的代码如下。
func addSubviews()
self.homeLogo = UIImageView()
self.homeLogo!.contentMode = UIViewContentMode.ScaleAspectFit
self.addSubview(self.homeLogo!)
self.orderBtn = PPButton(title: "Order")
self.orderBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.orderBtn!)
self.findUsBtn = PPButton(title: "Find Us")
self.findUsBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.findUsBtn!)
self.menuBtn = PPButton(title: "Menu")
self.menuBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.menuBtn!)
self.clubPatronBtn = PPButton(title: "Club Patron")
self.clubPatronBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.clubPatronBtn!)
self.loginButton = UIButton()
self.loginButton!.setTitle(String("Login or Create an Account").uppercaseString, forState: .Normal)
self.loginButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.loginButton!.titleLabel?.font = UIFont(name: "Formata-BoldCondensed", size: 18.0)
self.loginButton!.userInteractionEnabled = true
self.loginButton!.addTarget(self, action: Selector("testFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(self.loginButton!)
当我尝试单击按钮时,目标不会触发 PPButton。此外,它们不会触发 loginButton,它只是一个简单的 UIButton。所有这些都是以编程方式添加的。我确定我做错了一些小事。
另外,我从 ViewController 开始检查我的所有视图,哪些视图的 userInteractionEnabled 设置为 true。函数和输出如下。请帮忙!
func logViewHierarchy(view: UIView, num: Int)
var index = num
for i in 0..<index
print("\t")
index = index + 1
println("\(NSStringFromClass(view.dynamicType)) userInteractionEnabled: \(view.userInteractionEnabled)")
for subview in view.subviews
self.logViewHierarchy(subview as! UIView, num: index)
输出
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
_UILayoutGuide userInteractionEnabled: true
_UILayoutGuide userInteractionEnabled: true
Pizza_Patron.HomeView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: false
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
【问题讨论】:
【参考方案1】:所以看起来您的***视图以及一些较低的视图都将userInteractionEnabled
设置为false
。这会向下级联,因此如果超级视图关闭了交互,则其子视图将不会接收交互事件。
如果您想禁用某些视图的交互,您可以重新排序层次结构,只需为这些超级视图打开交互或尝试类似的操作并传递事件:
How to get touches when parent view has userInteractionEnabled set to NO in ios
【讨论】:
我所读到的所有内容实际上都与您告诉我的不同。如果我在顶层将 UserInteractionEnabled 设置为 true,则该对象将拦截所有触摸事件。我想从那时起,我应该覆盖 touchesBegan 或其他东西。 是的!我的父堆栈视图已禁用它。为我解决了问题。以上是关于尽管 userInteractionEnabled = true,但 UIView 中的 UIButton 没有响应触摸事件的主要内容,如果未能解决你的问题,请参考以下文章
试图操纵 UISlider 的 userInteractionEnabled 属性:不工作
在 overlaySKScene 上设置 userInteractionEnabled=true/false 无效
userInteractionEnabled = YES时如何将触摸事件传递给superview?
iOS bug 日志-userInteractionEnabled