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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With