Are NSLayoutConstraints animatable? [duplicate] Are NSLayoutConstraints animatable? [duplicate] ios ios

Are NSLayoutConstraints animatable? [duplicate]


Just follow this exact pattern:

self.heightFromTop.constant = 550.0f;[myView setNeedsUpdateConstraints];[UIView animateWithDuration:0.25f animations:^{   [myView layoutIfNeeded];}];

where myView is the view where self.heightFromTop was added to. Your view is "jumping" because the only thing you did in the animation block was to set the constraint, which does not cause layouts immediately. In your code, the layout happens on the next run loop after you set heightFromTop.constant, and by that time you are already outside the scope of the animation block.

In Swift 2:

self.heightFromTop.constant = 550myView.setNeedsUpdateConstraints()UIView.animateWithDuration(0.25, animations: {   myView.layoutIfNeeded()})


The Apple's suggested way is a little bit different (See example in "Animating Changes Made by Auto Layout" section). First you need to call layoutIfNeeded before the animation. Then add your animation stuff inside the animation block and then call layoutIfNeeded at the end again. For the guys like me who are transitioning to autolayout, its more similar way to previous animations that we were doing with the frames inside animation blocks. We just need to call layoutIfNeeded twice - before animations and after animations:

[self.view layoutIfNeeded]; // Ensures that all pending layout operations have been completed[UIView animateWithDuration:1.0f animations:^{  // Make all constraint changes here  self.heightFromTop.constant= 550.f;  [self.view layoutIfNeeded]; // Forces the layout of the subtree animation block and then captures all of the frame changes}];


I tried @Centurion's approach, but somehow my view would animate to a wrong frame if it's loaded from the storyboard. The issue goes away if I replace the first layoutIfNeeded with updateConstraintsIfNeeded, though I have no idea why. If anyone can give an explanation it'd be much appreciated.

[self.view updateConstraintsIfNeeded];[UIView animateWithDuration:1.0 animations:^{    self.myConstraint.constant= 100;    [self.view layoutIfNeeded];}];