使用setTranslatesAutoresizingMaskIntoConstraints(false)在UIView中添加为子视图时未调用UIButton操作

时间:2014-11-11 03:34:41

标签: ios swift uiview uibutton

当添加为UIView的子视图时,UIButton“按下”func未被调用。代码如下。然而,当我删除setTranslatesAutoresizingMaskIntoConstraints(false)时它的工作。我需要使用此方法进行自动布局调整大小。

var myView = UIView()
let orderBook = UIButton()

override func viewDidLoad() {
        super.viewDidLoad()

        myView.backgroundColor = UIColor.redColor()
        myView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(myView)

        let views1 = ["myView" : myView]

        var constV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myView(>=100)]-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views1)

        var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:[myView(==100)]|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views1)

        self.view.addConstraints(constH)
        self.view.addConstraints(constV)


        orderBook.setTitle("Order Book", forState: UIControlState.Normal)
        orderBook.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        orderBook.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)

        myView.addSubview(orderBook)
}
func pressed(sender: UIButton!) {
   println("pressed")
}

override func viewDidLayoutSubviews()
{
    orderBook.frame = CGRectMake(0, 0, myView.frame.width, 100)      
}

1 个答案:

答案 0 :(得分:3)

我知道我的回复有点迟了,但无论如何我仍会对那些仍然遇到此问题的人做出回应,或者提出解决方法。

在以编程方式将UIButton添加到UIVut之后无法与UIButton交互的原因是因为您尚未向UIButton添加所有必需的约束。在我的特定情况下,我没有添加宽度/高度约束。下面是我以编程方式创建的按钮的示例,我添加到也以编程方式创建的UIView。注意我添加到按钮的约束。

    var popupView = UIView()
    popupView.setTranslatesAutoresizingMaskIntoConstraints(false)
    popupView.backgroundColor = Color.fromHex("#FFFFFF", alpha: 1)
    popupView.layer.cornerRadius = 20
    popupView.clipsToBounds = true

    var bottomView = UIView(frame: CGRectMake(0, 0, popupView.frame.size.width, 80))
    bottomView.setTranslatesAutoresizingMaskIntoConstraints(false)

    var singleBtn = UIButton()
    singleBtn.titleLabel?.font = UIFont(name: "Nunito-Regular", size: 20)
    singleBtn.setTitleColor(Color.fromHex("#979797", alpha: 1), forState: UIControlState.Normal)
    singleBtn.setTitleColor(Color.fromHex("#56DAF0", alpha: 1), forState: UIControlState.Highlighted)

    singleBtn.setTitle("OK", forState: UIControlState.Normal)
    singleBtn.addTarget(self, action: "singleBtnAction:", forControlEvents: UIControlEvents.TouchUpInside)
    singleBtn.setTranslatesAutoresizingMaskIntoConstraints(false)
    singleBtn.backgroundColor = UIColor.redColor()
    bottomView.addSubview(singleBtn)

    bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
    bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
    bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))