간단한 레이아웃을 프로그래밍 방식으로 시도하고 간단한 것을 놓치거나 무언가 이상한 점이 있다고 생각합니다. 다음 ViewController는 슈퍼 뷰에서 레이블을 가운데에 맞춰야합니다. 그러나이 트리밍 된 메시지와 함께 충돌합니다. The view hierarchy is not prepared for the constraint ... When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled... View not found in container hierarchy: ... That view's superview: NO SUPERVIEW
이 오류 메시지가있는 다른 SO 질문은 대부분이 코코아 콩을 사용하고 있으며이를 피하려고합니다 또는 swift
대신 Obj-C
을 사용하십시오. This 질문은 약간의 주제를 다루지 만 약간 낡은 질문입니다.신속한 VFL 제약 : 슈퍼 뷰가 없기 때문에 충돌이 발생했습니다.
누구나 올바른 방향으로 나를 가리킬 수 있습니까?
class ViewController: UIViewController {
let label1 = UILabel() as UILabel
func layoutView(){
label1.text = "Click to see device configuration"
label1.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(label1)
let viewsDictionary = ["label1":label1]
let label1_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[label1]-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
let label1_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label1]-|",
options: NSLayoutFormatOptions(0),
metrics: nil, views:
viewsDictionary)
label1.addConstraints(label1_H) // Comment these 2 lines and it runs, but
label1.addConstraints(label1_V) // of course the label is upper left
}
override func viewDidLoad() {
super.viewDidLoad()
layoutView()
}
}
고맙습니다 - 예를 들어 보았습니다. 과정을 잘 이해하지 못했습니다. –