简体   繁体   中英

How to remove constraints from all subviews inside my superview?

I have a UIView that contains multiple UIView subviews that has its own constraints. How do I remove the subviews' constraints?

//only removes the constraints on self.view
[self.view removeConstraints:self.view.constraints];

//I get warning: Incompatible pointer types sending 'NSArray *' to parameter of type 'NSLayoutConstraint'
[self.subview1 removeConstraints:self.subview1.constraints];

Try this code:

for (NSLayoutConstraint *constraint in self.view.constraints) {
    if (constraint.firstItem == self.subview1 || constraint.secondItem == self.subview1) {
        [self.view removeConstraint:constraint];
    }
}

Basically, this iterates all of the constraints that are assigned to self.view and checks to see whether self.subview1 is involved in the constraint. If so, that constraint gets pulled.

You have to remove all the constraints of the view and its subview. So create an extension of UIView and then define the following method:

extension UIView {
    func removeAllConstraints() {
        self.removeConstraints(self.constraints)
        for view in self.subviews {
            view.removeAllConstraints()
        }
    }
}

then call the following:

self.view.removeAllConstraints()

As answering later, This is in swift. This may help you.

I wrote an extension on UIView:

extension UIView{

    func removeConstraintsFromAllEdges(of view: UIView){

        for constraint in constraints{
            if (constraint.firstItem.view == view || constraint.secondItem?.view == view){
                removeConstraint(constraint)
            }
        }
    }

    func addConstraintsToAllEdges(of view: UIView){
        let leading = leadingAnchor.constraint(equalTo: view.leadingAnchor)
        let top = topAnchor.constraint(equalTo: view.topAnchor)
        let trailing = trailingAnchor.constraint(equalTo: view.trailingAnchor)
        let bottom = bottomAnchor.constraint(equalTo: view.bottomAnchor)

        NSLayoutConstraint.activate([leading, top, trailing, bottom])
    }
}

Interesting note is secondItem is an optional property of NSLayoutConstraint while firstItem is non-optional. Why? Because sometimes you may need to constrain a view's height to a constant value, so you there is no other view involved.

I took @Honey's answer and modified it to work so a view can remove the constraints in relation to his superview.

extension UIView{

    func removeConstraintsFromAllEdges(){

        if let superview = superview {

            for constraint in superview.constraints{

                if let firstItem = constraint.firstItem, firstItem === self {
                    superview.removeConstraint(constraint)
                }

                if let secondItem = constraint.secondItem, secondItem === self {
                    superview.removeConstraint(constraint)
                }

            }

        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM