Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing two modal view controllers

I have found several questions about this but the answers do not resolve my problem.

I have two controllers that I have presented using presentModalViewController.

I added modalTransitionStyle to the first controller that is called by Main Controller. The first controller presented the second controller normally (without transition style).

FirstVC *first = [[FirstVC alloc] initWithNibName:@"FirstVC" bundle:nil];
first.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:first animated:YES];

SecondVC *second = [[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
[self presentModalViewController:second animated:YES];

This is the code I used to go to the MainVC:

[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];

And this is what happened:

enter image description here

The page didn't uncurl. What's the reason I'm encountering this?

like image 646
Lie-An Avatar asked Aug 29 '13 03:08

Lie-An


People also ask

How do you dismiss a modal view controller?

The first option is to tell the view to dismiss itself using its presentation mode environment key. Any view can read its presentation mode using @Environment(\. presentationMode) , and calling wrappedValue. dismiss() on that will cause the view to be dismissed.


1 Answers

In my experiments, it appears you can't have a standard presentation (cover vertical) after a partial curl, and dismiss them both at the same time, unless you do the dismissal with the animation set to NO.

A way to fix this though is to dismiss secondVC with no animation (this code is in secondVC):

-(IBAction)dismissSelf:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

Then in the firstVC dismiss again in viewDidAppear with animation, after testing that the controller is not being presented:

-(void)viewDidAppear:(BOOL)animated {
    if (![self isBeingPresented]) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

While the above code works to get back to the initial controller's view, you will see firstVC's view appear before the curl uncurls. If you don't want to see that, then the only way I could find to fix that, was to create an image of secondVC, add that as (in an image view) a subview to firstVC before doing the dismissal of secondVC. So, to do that, the code in secondVC should be this instead (note that you have to link to QuartzCore and import it into secondVC for this to work):

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIImage *img = [self imageWithView:self.view];
    FirstViewController *first = (FirstViewController *)self.presentingViewController;
    UIImageView *iv = [[UIImageView alloc] initWithFrame:first.view.bounds];
    iv.image = img;
    [first.view addSubview:iv];
}


-(IBAction)dismissSelf:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}


- (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
like image 188
rdelmar Avatar answered Sep 18 '22 05:09

rdelmar