0
votes

I'm adding thumbnailMaskView on top of thumbnailImageView and it works!

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)])
}

I'm trying to extract that code to a UIView Extension with the following code:

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)])
    }
}

When using the extension function thumbnailImageView.addOnTop(thumbnailMaskView) I get the error:

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:>'

1
How do you use addOnTop ??Sh_Khan

1 Answers

0
votes

To add constraints to a view then those should be for a child/descendant view , so it's better to use 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) 
])

And it will add the constraints to the correct view