Để cho giả UIImageView
của bạn được thêm vào trong đầu khi bạn đưa vào hình ảnh của bạn ở trên, sau đó bạn có thể thêm các ràng buộc theo chương trình như thế nào trong các cách sau:
override func viewDidLoad() {
super.viewDidLoad()
// assuming here you have added the self.imageView to the main view and it was declared before.
self.imageView.setTranslatesAutoresizingMaskIntoConstraints(false)
// create the constraints with the constant value you want.
var verticalSpace = NSLayoutConstraint(item: self.imageView, attribute: .Bottom, relatedBy: .Equal, toItem: self.button, attribute: .Bottom, multiplier: 1, constant: 50)
// activate the constraints
NSLayoutConstraint.activateConstraints([verticalSpace])
}
Trong đoạn mã trên tôi chỉ đặt các ràng buộc không gian dọc, bạn cần phải thiết lập các ràng buộc cần thiết để tránh cảnh báo về nó.
Có một số cách để thêm ràng buộc theo chương trình bạn có thể đọc thêm trong câu trả lời rất hay này SWIFT | Adding constraints programmatically.
Tôi hy vọng điều này sẽ giúp bạn.
Nguồn
2015-07-21 15:47:24
bạn cũng phải thêm trở ngại lập trình https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/#//apple_ref/occ/clm/NSLayoutConstraint/constraintWithItem:attribute:relatedBy: toItem: thuộc tính: hệ số: hằng số: –