3

I have a UIButton created programmatically, added the "pressed" function for the event "UIControlEvents.TouchUpInside". But the "pressed" method is not called when added as the subview of an UIView. Code is provided below for your reference. However, it works when I removed the setTranslatesAutoresizingMaskIntoConstraints(false). I need to use this method for auto layout resizing.

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)      
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
kaneyip
  • 1,237
  • 1
  • 17
  • 21
  • Try to set userintraction enable "YES" for subviews – srinivas n Nov 11 '14 at 03:38
  • i has set myView.userInteractionEnabled = true. However it still not working. – kaneyip Nov 11 '14 at 03:43
  • You should set `frame` for `orderBook` button, or add appropriate constraints. – rintaro Nov 11 '14 at 03:47
  • i did set the orderBook frame. Has edited my question. I forget to copy it to stackflow. Its still not working. – kaneyip Nov 11 '14 at 03:57
  • Check the size of `myView`. Any point in a subview that is outside of its superview can’t receive touch events because the touch point has to be within the bounds of the superview and the subview. – linimin Nov 11 '14 at 05:08

1 Answers1

3

I understand that my response is a bit late, but I'll respond anyway for those that are still having this issue, or have come up with a workaround.

The reason why you are unable to interact with the UIButton after you have added it to a UIView programmatically is because you have not added all of the necessary constraints to the UIButton. In my particular case I didn't add the Width/Height constraint. Below is an example of a button I created programmatically to which I added to a UIView also created programatically. 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))
Dondrey Taylor
  • 290
  • 3
  • 10
  • 1
    Looking at my problem from this angle helped me out, thanks. The only difference is that in my case it wasn't the UIButton that was not fully specified but rather the containing view which was not fully specified even though it looked correct on the screen (I was missing a "top" constraint) – moliveira Feb 04 '16 at 16:39