1

I have a complex autolayout which needs to adapt to rotation by removing old constraints and adding new ones.

I have a grid layout of views inside a containerView.

The problem is when I call

  [UIView animateWithDuration:0.4 animations:^{
            [self.containerView layoutIfNeeded];
    }];

The views always animate from the position [0,0] to their new position. What I would prefer is that the views would move from their old position (before I remove the constraints) to the new positions (after removal and addition of the new constraints)

Has someone been able to achieve this?

Aranir
  • 828
  • 1
  • 10
  • 21
  • How about adding the constraint manipulation to the animation block? – David Berry May 01 '14 at 15:42
  • 1
    Are you doing this in `viewDidLoad`? The initial constraints have not yet been applied and hence 0,0. If you do this in `viewDidAppear`, you should see them animate from old position to new position. If this is a rotation based animation, in which method are you trying to invoke this? Also, after removing the old constraints, are you doing anything that might trigger the (non-existent) constraints to be applied (which can happen with the most innocuous of actions, such as setting text label)? A little more context about where you've put this code might be helpful. – Rob May 01 '14 at 15:54

1 Answers1

1

The following post gave me the hint to make it work:

But not the actual accepted answer but rather:

[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{
    [self.view layoutIfNeeded];
} completion:nil];

The optional parameter options:UIViewAnimationOptionLayoutSubviews inspired me to try different options and the actual option needed was options:UIViewAnimationOptionBeginFromCurrentState

[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        [self layoutIfNeeded];
    } completion:nil];

Which allowed the animation to occur from the last position, even if constraints are removed and new ones are added.

Community
  • 1
  • 1
Aranir
  • 828
  • 1
  • 10
  • 21