UIButton action not called when added to UIView using setTranslatesAutoresizingMaskIntoConstraints (false)
UIButton "clicked" func not called when added as a UIView subroutine. Code as shown below. However its working when removing setTranslatesAutoresizingMaskIntoConstraints (false). I need to use this method to automatically resize the layout.
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)
}
source to share
I realize that my answer is a bit late, but I will still answer for those who have this problem or have come up with a workaround.
The reason why you cannot interact with the UIButton after you have added it to the UIView programmatically is because you have not added all the required constraints to the UIButton. In my particular case, I have not added a width / height constraint. Below is an example of a button that I created programmatically, which I added to the UIView, also created programmatically. Notice the constraints I added to the button.
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))
source to share