Container view disappearing on completeTransition:
Asked Answered
K

2

19

I am using custom view controller transitions, UIViewControllerAnimatedTransitioning, to present and dismiss a view controller.

The presenting animation works fine, but when I run the dismiss animation, once I call completeTransition: the containerView gets removed.

I'm not sure what is going on, here is the transition code:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *containerView = [transitionContext containerView];
    containerView.backgroundColor = [UIColor blackColor];

    if (self.reverse) {
        [containerView addSubview:toViewController.view];
        [containerView addSubview:fromViewController.view];
    } else {
        [containerView addSubview:fromViewController.view];
        [containerView addSubview:toViewController.view];
    }

    if (! self.reverse) { // Forward
        toViewController.view.frame = CGRectMake(-containerView.frame.size.width, 0, containerView.frame.size.width, containerView.frame.size.height);
    } else {
        fromViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
    }

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
        if (self.reverse) {
            fromViewController.view.frame = CGRectMake(-containerView.frame.size.width, 0, containerView.frame.size.width, containerView.frame.size.height);
            fromViewController.view.layer.opacity = 0.f;
            toViewController.view.layer.opacity = 1.f;
        } else {
            toViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
            toViewController.view.layer.opacity = 1.f;
            fromViewController.view.layer.opacity = 0.3f;
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:finished];
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.reverse) {
        return 0.45;
    } else {
        return 0.35;
    }
}

How can I prevent my toViewController from disappearing if .reverse is set to YES?

Update: This is how I'm presenting the view controller:

SecondaryViewController *vc = [[SecondaryViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.modalPresentationStyle = UIModalPresentationCustom;
navigationController.transitioningDelegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
Keos answered 18/7, 2014 at 16:58 Comment(1)
How can I keep the container view——prevent the toViewController from disappearing.Keos
H
57

The container view disappearing on dismissal is correct behavior. Your mistake is adding the fromView to it.

You are incorrectly distinguishing whether this is presentation or dismissal and what you should do in each case. Simply use the two view controllers fromViewController and toViewController to tell them apart; on dismissal, the roles are reversed. On dismissal, do not add anything to the content view; the original presenter is still present and will be revealed by the removal of the container view.

So, on presentation, add only the toView to the container view. On dismissal, do not add anything to the container view. It's as simple as that.

Haphazard answered 18/7, 2014 at 17:41 Comment(16)
If I just don't add fromViewController.view to the containerView, on the reverse animation, the toViewController still disappears.Keos
Is this a navigation controller animation, a tab bar controller animation, or a presented view controller animation?Haphazard
I update the question with the code I use to present the view controller.Keos
Are you using iOS 7 or are you experimenting with iOS 8? Things are different in iOS 8 so you might need to modify your code still further.Haphazard
I got this to work with your most updated answer on iOS 8. ThanksKeos
For iOS 8, here's some working code that will help you: github.com/mattneub/Programming-iOS-Book-Examples/blob/master/… Here's an earlier iOS 7 version of the same thing: github.com/mattneub/custom-alert-view-iOS7/blob/master/…Haphazard
This only happened to me on iOS 8, not 7, but this definitely fixed it. Guessing the previous behavior working in iOS 7 was only coincidental.Tar
This is hugely frustrating. I've found that if you are using presentViewController/dismissViewController you need to add the view on dismissal however if you use the same transition on a UINavigationController you don't add the view. Any info?Constrain
@Constrain Not sure what you're asking. If you have a question, ask it as a question. Or read my book, or look at the examples from my book; this is all there.Haphazard
@Haphazard More just adding the information for others. It's an inconsistency that's not obvious (or wasn't to me). My solution was to check for toViewController.navigationController in the animateWithDuration:animations:completion: completion block and add the toView to the keyWindow only when not inside a navigation controller. If this sounds off base I'd appreciate a pointer to a code example.Constrain
this saved my week and a half. If only I had seen it earlier! Thanks!Talca
"On dismissal, do not add anything to the content view; the original presenter is still present and will be revealed by the removal of the container view". Thank you for this! When reading Apple's docs it is clearly documented that the system adds the PRESENTING VC by default. For some stupid reason I translated that into the fromViewController, no matter whether we're talking about presentation or dismissal, and spend an hour trying to find a solution to a problem of mine, until I found this post.Laina
This contradicts the documentation: "Add the view being presented (or revealed if the transition involves dismissing a view controller) to the container view’s hierarchy and set up any animations you want to make that view move into position." developer.apple.com/reference/uikit/… I have transitions for which I have to add the controller during dismissal and I have some for which I don't. My advice is to handle them case by case.Nievelt
@Haphazard Why can we assume the presentation was not fullscreen?Nievelt
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) is betterAlejandraalejandrina
Thank you man! This was the proper comprehensive answer I have been looking for!Gomar
A
1

My solution is setting modalPresentationStyle = .custom. It can be before or after the line transitioningDelegate = self.

TLDR:

This discussion regards to presenting transition only, because I didn't have much issue with dismissing transition.

My modal view is smaller than the screen and the presenting view is supposed to be shown in the background. But no, I got a totally black background instead of the presenting view as soon as transitionContext.completeTransition is called. I have a similar modal view with the presenting view in the background somewhere else in code, and it's totally working. After doing some comparison I eliminate all the other possibilities and narrow down to the difference of modalPresentationStyle = .custom, which has .fullScreen for the default value. I guess .fullScreen assumes the presented view takes full screen and decide to remove the presenter view (fromView) regardlessly.

One thing interesting with modalPresentationStyle = .custom: transitionContext.view(forKey: .from) returns nil while transitionContext.viewController(forKey: .from) still returns the presenting view controller.

Apparel answered 1/8, 2018 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.