Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to invalidate uiview constraints

I would like to invalidate a uiview constraints from code to force all fields to relayout based on a specific change in the view.

For example, suppose i have two UILabels that are given a fixed VerticalSpace constraint. And in some user event i am resizing the label that is on top. I would like to be able to force the constraint to invalidate from code.

This is the first time i use constraints and i do not understand how i can do this from code. I know that when the device is rotated constraints on UIViews are invalidated.

like image 366
Marc Matta Avatar asked Sep 06 '25 05:09

Marc Matta


1 Answers

You call setNeedsLayout on the relevant view, typically your view controller's .view. :

[self.view setNeedsLayout];

You're not really invalidating the constraints, that is a slightly different thing used for custom views which may need to remove and recalculate constraints based on other changes. What you're doing is letting the view know that some of the factors it used to calculate its layout have now changed. The constraints are still valid, but the results have changed.

To animate to your new layout, you can call layoutIfNeeded inside an animation block.

like image 158
jrturton Avatar answered Sep 07 '25 21:09

jrturton