尽管userInteractionEnabled = true,UIView中的UIButton仍未响应触摸事件

时间:2015-07-30 21:44:43

标签: ios iphone xcode uiview uibutton

我在这个问题上遇到了困难。我有一个自定义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!)
}

当我尝试单击按钮时,目标不会为PPButtons触发。此外,它们不会为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 个答案:

答案 0 :(得分:1)

因此看起来您的顶级视图以及一些较低级别的视图将userInteractionEnabled设置为false。如果超级视图关闭了交互,则其级联向下,其子视图不会收到交互事件。

如果您希望禁用某些视图的互动,只需为这些视图启用互动或尝试使用此类视频并传递事件,您就可以重新排序层次结构:

How to get touches when parent view has userInteractionEnabled set to NO in iOS

相关问题