How to remove constraints from all subviews inside

2019-07-04 22:05发布

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];

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-04 22:16

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-04 22:16

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.

查看更多
一夜七次
4楼-- · 2019-07-04 22:23

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.

查看更多
beautiful°
5楼-- · 2019-07-04 22:26

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

            }

        }

    }

}
查看更多
登录 后发表回答