视图层次结构未准备好

时间:2016-02-25 20:59:57

标签: ios swift

我是一名新手swift开发人员,在一本优秀的iOS 9 Swift书中提供了代码示例。本书刚刚演示了如何在视图层次结构的两个不同分支中的两个对象(标签,按钮)之间添加约束(在代码中)。

myLabel位于viewBB容器中,位于顶视图控制器中。 myButton位于viewBB之外的顶视图控制器中。

示例代码运行正常。但是对于练习,我想在viewBB中创建第二个label2,并将其位置约束为偏离myLabel。所以这两个标签(我认为)都在viewBB中。

在代码中创建label2并在代码中添加约束可以正常工作。

但是......如果我在界面构建器中创建label2,并尝试删除IB约束并将其替换为我自己的约束,则构建成功但应用程序崩溃,而通常的"视图层次结构未准备好&#34 ;错误。

***有趣的是关于标签和按钮的错误消息,建议书示例代码(我在下面称之为BLOCK 0)是罪犯。

我做错了什么? (我已经阅读了我在错误消息中找到的所有内容,包括此论坛中的4个帖子。但我不知道为什么图书代码失败了。)

错误消息:

2016-02-25 12:52:32.796 TwoViewConstraints [5713:1860768] 视图层次结构没有为约束准备:NSLayoutConstraint:0x7c9ce990 UILabel:0x7c9dbec0' Label' .centerX == 的UIButton:0x7c9deed0'按钮' .centerX

当添加到视图时,约束的项必须是该视图的后代(或视图本身)。如果在组装视图层次结构之前需要解析约束,则会崩溃。打破 - [UIView(UIConstraintBasedLayout)_viewHierarchyUnpreparedForConstraint:]进行调试。

来自调试器的消息:由于信号15而终止

这是我的代码:

class ViewController: UIViewController {

@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myButton: UIButton!
@IBOutlet weak var centerConstraint: NSLayoutConstraint!
@IBOutlet weak var viewBB: UIView!


@IBOutlet weak var lab2cxh: NSLayoutConstraint!
@IBOutlet weak var lab2cxv: NSLayoutConstraint!
@IBOutlet weak var label2: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // BLOCK 0 - works fine, from the iOS9 book
    // myLabel and myButton were created with the interface builder
    // Goal is to remove an IB constraint, and replace it with one
    // that is built in code. (Because label and buttons are in 
    // different branches of the view hierarchy).
    //
    // remove auto center constraint from parent label
    viewBB.removeConstraint(centerConstraint)

    // center the label and the button across views
    let ccx = NSLayoutConstraint(item: myLabel,
        attribute: NSLayoutAttribute.CenterX,
        relatedBy: NSLayoutRelation.Equal,
        toItem: myButton,
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0,
        constant: 0)
    self.view.addConstraint(ccx)

//        BLOCK 1 - Create a second label object in code
//        this code works fine with the two added constraints below
//        create a second label object and add it to the view
//        let label2 = UILabel()
//        label2.translatesAutoresizingMaskIntoConstraints = false
//        label2.text="Label2"
//        viewBB.addSubview(label2)

    // BLOCK 2 - create a second label object in the interface builder
    // Suggested horiz/vert constraints are set in the IB.
    // So now I want to remove them, and replace them with my own,
    // as was done in the Swift book example code in BLOCK 0.
    // remove original label 2 constraints
    viewBB.removeConstraint(lab2cxv)
    viewBB.removeConstraint(lab2cxh)

    // adding these two constraints works fine when the label2 object
    // is created in code. But when I create label2 in the interface
    // builder and try to remove the IB constraints (as was done in the
    // first block of code), I get the error:
    //
    // ... "The view hierarchy is not prepared
    // for the constraint "Label.CenterX to Button.CenterX", which is 
    // the first block of code.
    //
    // NOTE** To me, it looks like the error is coming from the 
    // BLOCK 0 constraint, since it cites a label and a button, not
    // two labels. 
    // What am I doing wrong?
    let c2h = NSLayoutConstraint(item: label2,
        attribute: NSLayoutAttribute.CenterY,
        relatedBy: NSLayoutRelation.Equal,
        toItem: myLabel,
        attribute: NSLayoutAttribute.CenterY,
        multiplier: 1.0,
        constant: -50)
    self.viewBB.addConstraint(c2h)

    // constrain label 2 to be diagonally left of label one
    let c2v = NSLayoutConstraint(item: label2,
        attribute: NSLayoutAttribute.CenterX,
        relatedBy: NSLayoutRelation.Equal,
        toItem: myLabel,
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0,
        constant: -100)
    self.viewBB.addConstraint(c2v)


}

1 个答案:

答案 0 :(得分:0)

我从头开始重新编写了整段代码,并且工作正常。去搞清楚。这是正常工作的代码。据我所知,它几乎是相同的代码,除了在这里和那里的小名称更改。

我不知道为什么这个版本有效,而之前的版本没有。这让我想知道Interface Builder设置中是否存在不同之处。

class ViewController: UIViewController {

@IBOutlet weak var viewbb: UIView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var centerConstraint: NSLayoutConstraint!
@IBOutlet weak var myButton: UIButton!

@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label2centerConstraint: NSLayoutConstraint!
@IBOutlet weak var label2Yconstraint: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // remove the IB centering constraint, then add our own between label and button
    viewbb.removeConstraint(centerConstraint)

    // make a new constraint to center label over button
    let newcon = NSLayoutConstraint(item: myLabel,
        attribute: NSLayoutAttribute.CenterX,
        relatedBy: NSLayoutRelation.Equal,
        toItem: myButton,
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0,
        constant: 0)
    self.view.addConstraint(newcon)

    // remove the IB centering constraint, then add our own between label and button
    viewbb.removeConstraint(label2centerConstraint)
    viewbb.removeConstraint(label2Yconstraint)

    // make a new constraint to center label over button
    let newcon2 = NSLayoutConstraint(item: label2,
        attribute: NSLayoutAttribute.CenterX,
        relatedBy: NSLayoutRelation.Equal,
        toItem: myLabel,
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0,
        constant: -100) // left of mylabel

    // make a new constraint to center label over button
    let newcon3 = NSLayoutConstraint(item: label2,
        attribute: NSLayoutAttribute.CenterY,
        relatedBy: NSLayoutRelation.Equal,
        toItem: myLabel,
        attribute: NSLayoutAttribute.CenterY,
        multiplier: 1.0,
        constant: 0) // left of mylabel

    self.view.addConstraint(newcon3)
    self.view.addConstraint(newcon2)
}