加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:06:51 所属栏目:百科 来源:网络整理
导读:override func viewDidLoad() { super.viewDidLoad() self.blurView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight)) as UIVisualEffectView self.view.insertSubview(self.blurView,belowSubview: self.filterPanel) self.blurView.addCon
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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读