I modified programmatically one autolayout constraint. It takes effect on one certain view, but other views that are bound with other constraints to that view, do not change their positions. Is there an "updateAllConstraints" method?
4 Answers
Call those two methods on the view you want to be updated:
-setNeedsLayout
-layoutIfNeeded
The first one says to the layout system that this view needs to be laid out, because it has some changes and, everything should be recalculated. The second force the layout system to be run now, layout system is triggered at specific times during runtime, with this method you are saying:"do it now".
-
Can i change constrains of an Imageview through UIButton click? If so, How please explain as i am new in this.– SofedaMay 19, 2016 at 8:57
-
-
1@Sofeda sure you can. In your UIButton's action method you could remove the constraints of the UIImageView with e.g.
view.removeConstraints(imageView)
and then set new constraints.– BaranSep 1, 2018 at 11:44 -
The code given in the answer will also recalculate the constraints even if the view is not attached to the hierarchy Dec 9, 2020 at 3:18
Yes There is a method on UIView called - (void)updateConstraintsIfNeeded
However I don't think this is your problem
The problem was that constrains were not set up properly, and in this case non of the setNeedsLayout or updateConstraintsIfNeeded can help.
Those of you that come across this topic, and are looking where to find this in the menus.
Best I have found is:
Select the items that you want to "pop" back to your constraints
Top menu: Editor/Update Frames (or/) shortcut: command + option + =
Hopefully this helps