let golabel = UILabel()
golabel.setTranslatesAutoresizingMaskIntoConstraints(false)
let horizontalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 30)
view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 100)
view.addConstraint(verticalConstraint)
let widthConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 350)
golabel.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
golabel.addConstraint(heightConstraint)
來源
2015-06-19 06:58:54
agy
Autolayout關於將您的視圖與其他視圖相關聯。如果你沒有解釋它應該如何與你的界面相關聯是不可能給你的答案 – Andrea
我希望它被放置在中心的上方 – tanman
X或Y中心?或兩者? – Andrea