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)
}