5
votes

We can change constant property of NSLayoutConstraint, and than use UIView animation block to make the change animated.

myLayoutConstraint.constant = 50;
[UIView animateWithDuration: .3 animations:^{
    [view layoutIfNeeded];
}];

However, the subviews of the view, which has layout constraints as their super view, will be animated! They will animate from original zero frame to the target frame of autolayout. How to solve this problem?

2

2 Answers

12
votes

This suggests that you have pending layout operations. Call layoutIfNeeded once before

myLayoutConstraint.constant = 50;
0
votes

If the sub views have constraints that depend on the animated constraint then no this isn't possible.

What you can do though is make the constraints of the sub views independent of the animating constraint.

So, if you are animating the height of the preview then don't pin the sub views to the top AND bottom of their superview as this will make them change while it is animating. Only pin them to the top so that they "fall" off the bottom.

That way when the height is animated it will reveal the sub views in place without animating them.