似乎標籤位置不正確,因爲它沒有任何約束。
你可以試試下面的代碼嗎?
label.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: textField, attribute: .Top, multiplier: 1.0, constant: 0.0)
let leftConstraint = NSLayoutConstraint(item: label, attribute: .Left, relatedBy: .Equal, toItem: textField, attribute: .Left, multiplier: 1.0, constant: 0.0)
let bottomConstraint = NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: textField, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
let rightConstraint = NSLayoutConstraint(item: label, attribute: .Right, relatedBy: .Equal, toItem: textField, attribute: .Right, multiplier: 1.0, constant: 0.0)
self.view.insertSubview(label, belowSubview: textField)
NSLayoutConstraint.activateConstraints([topConstraint, leftConstraint, bottomConstraint, rightConstraint])
您是否正確設置了要添加的視圖的x:y座標?請注意,上面的插入是關於z順序的,而不是x:y的位置。 – Verticon