1.第一步,创建试图,添加到self.view中
var button1: UIButton? = UIButton(frame: CGRectMake(350, 20, 100, 50))
button1!.backgroundColor = UIColor(white:5, alpha: 0.5)
<span style="white-space:pre"> </span>self.view.addSubview(button1)
2.第二步
button1!.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addConstraint(NSLayoutConstraint(item: button1, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: .CenterX, multiplier: 1.0, constant: -120))
self.view.addConstraint(NSLayoutConstraint(item: button1, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: .CenterY, multiplier: 0, constant: 50))
3.效果