本文主要是介绍swift中NSLayoutConstraint布局的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.效果
这篇关于swift中NSLayoutConstraint布局的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!