Remove Auto-Layout Constraints for Specific Object

前端 未结 6 456
走了就别回头了
走了就别回头了 2020-12-24 05:16

I have a UIImageView embedded in a UIView. My entire app uses AutoLayout, but I want to remove constraints for the

6条回答
  •  甜味超标
    2020-12-24 05:45

    you can call

    [yourElement removeFromSuperview];
    

    and then call

    [yourSuperview addSubview:yourElement];
    

    to remove all constraints from yourElement and add it back to the view.

    It is worth mentioning that if you are doing the above on something like a UIView category or extension, you need to save a reference to the view's superview before removing from superview:

    var theSuperview = self.superview
    self.removeFromSuperview()
    theSuperview?.addSubview(self)
    

    You might also need to make yourElement strong instead of weak since removing from superview could potentially cause you to lose a reference to it.

提交回复
热议问题