在扩展 'The view hierarchy is not prepared for the constraint' 中获取错误

Getting the error in an extension 'The view hierarchy is not prepared for the constraint'

我在 thumbnailImageView 之上添加了 thumbnailMaskView,它起作用了!

func setupViews() {

    addSubview(thumbnailImageView)
    addSubview(thumbnailMaskView)

    addConstraintsWithFormat(format: "H:|[v0]|", views: thumbnailImageView)
    addConstraintsWithFormat(format: "V:|-16-[v0]-16-|", views: thumbnailImageView)

    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .leading, relatedBy: .equal, toItem: thumbnailImageView, attribute: .leading, multiplier: 1, constant: 0)])
    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .trailing, relatedBy: .equal, toItem: thumbnailImageView, attribute: .trailing, multiplier: 1, constant: 0)])
    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .top, relatedBy: .equal, toItem: thumbnailImageView, attribute: .top, multiplier: 1, constant: 0)])
    addConstraints([NSLayoutConstraint(item: thumbnailMaskView, attribute: .bottom, relatedBy: .equal, toItem: thumbnailImageView, attribute: .bottom, multiplier: 1, constant: 0)])
}

我正在尝试使用以下代码将该代码提取到 UIView Extension

extension UIView {

    func addOnTop(_ topView: UIView) {
        addConstraints([NSLayoutConstraint(item: topView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)])
        addConstraints([NSLayoutConstraint(item: topView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)])
        addConstraints([NSLayoutConstraint(item: topView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)])
        addConstraints([NSLayoutConstraint(item: topView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)])
    }
}

当使用 extension 函数时 thumbnailImageView.addOnTop(thumbnailMaskView) 我得到错误:

2019-10-04 14:19:13.149081+0100 Rich People[1589:18861] [LayoutConstraints] The view hierarchy is not prepared for the constraint: (inactive)> 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. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug. 2019-10-04 14:19:13.152718+0100 Rich People[1589:18861] [LayoutConstraints] View hierarchy unprepared for constraint. Constraint: Container hierarchy: > View not found in container hierarchy: > That view's superview: > 2019-10-04 14:19:13.160046+0100 Rich People[1589:18861] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint: (active)> view:>'

要向视图添加约束,那么那些应该用于 child/descendant 视图,因此最好使用 activate

NSLayoutConstraint.activate([  
    topView.leadingAnchor.constraint(equalTo: self.leadingAnchor), 
    topView.trailingAnchor.constraint(equalTo: self.trailingAnchor), 
    topView.topAnchor.constraint(equalTo: self.topAnchor), 
    topView.bottomAnchor.constraint(equalTo: self.bottomAnchor) 
])

并且它会将约束添加到正确的视图