[iOS]: Auto layout animate change of frame position when removing and adding constraints

StackOverflow https://stackoverflow.com/questions/23410455

  •  13-07-2023
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top