ios – 查看没有为约束准备的层次结构

前端之家收集整理的这篇文章主要介绍了ios – 查看没有为约束准备的层次结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
override func viewDidLoad() {
    super.viewDidLoad()

    self.blurView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight)) as UIVisualEffectView
    self.view.insertSubview(self.blurView,belowSubview: self.filterPanel)

    self.blurView.addConstraint(NSLayoutConstraint(item: self.blurView,attribute: NSLayoutAttribute.Top,relatedBy: NSLayoutRelation.Equal,toItem: self.view,multiplier: 1,constant: 0))
    self.blurView.addConstraint(NSLayoutConstraint(item: self.blurView,attribute: NSLayoutAttribute.Bottom,attribute: NSLayoutAttribute.Leading,attribute: NSLayoutAttribute.Trailing,constant: 0))
}

调用上面的代码时,它崩溃并出现错误“视图层次结构没有为约束做好准备”.我可以理解,如果我在尝试在将约束添加到视图之前添加约束但是插入了视图,然后添加了约束,则会生成错误.

有任何想法吗?

解决方法

需要将约束添加到所有相关视图的共同祖先(其中视图被视为其自身的祖先). self.blurView是self.view的子视图.约束涉及self.blurView和self.view.因此,需要将约束添加到self.view,而不是self.blurView.

顺便说一句,你的线索应该是错误信息的这一部分(你没有引用,但你的关联做了here):

When added to a view,the constraint’s items must be descendants of that view (or the view itself).

猜你在找的Xcode相关文章