简体   繁体   中英

Presenting a View Controller Modally Resets The Current View

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).

在此处输入图片说明

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] .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM