Presenting a View Controller Modally Resets The Cu

2019-03-04 19:27发布

Question

I am trying to present view controller modally (a UIImagePickerController to be precise) while animating some views before the presentation.

It's important to note that the animations and the presentation of the view are not chained, and there can be a time-gap between the views animating and modally presenting the image picker.

With all that said, while I present the view controller, it seems that all of the subviews of the current view I'm in (the view I'm presenting from - the parent view controller's view) are being reset to their original "storyboard" positions, which causes all of the animations I've done in the view BEFORE presenting the image picker to reset.

Another thing worth noting, is that I use AutoLayout to position the views I animate.

I've added a sample animation showing the problem - notice how the "Animate" button snaps back to it's original position right after I click the "Present Modal View Controller" button (I've toggled "Slow Animations" right before presenting the modal view controller so you can see the "Animate" button snaps back).

enter image description here

I'm also adding a link to the example app shown in the animation so you examine the problem more deeply, click here to download it.

Answer

To solve the issue, I used @kokx's answer, and animated the Auto Layout constraints of the views instead of their frame values. To do that, I simply created outlets for the constraints I wanted to modify, and modified the constant property of the constraints.

To animate the change, simply call the original [UIView animateWithDuration:options:animations:completion:] while replacing any animation code it's animations: part with a call to [self.view layoutIfNeeded].

1条回答
劫难
2楼-- · 2019-03-04 20:11

To solve that issue, you need to use NSLayoutConstraint as well as change Constraint instead of changing frames.

Please check attached video where, Issue is resolved. Video Link

Code will be as below:

 self.topcons.constant = self.topcons.constant - 220;
[UIView animateWithDuration:0.4f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     [self.view layoutIfNeeded];
                 } completion:nil];

Thanks

查看更多
登录 后发表回答