Search code examples
objective-cxcodeuiviewcontrolleruicontainerviewios12

Animate 2 UIViewControllers in a UIContainerView


Hello everyone I'm animating the display of 2 UIViewController inside a UIContainerView. For the presentation of the view controllers I implemented this method

#pragma mark - Animation Controller
-(void)moveViewControllerFrom:(UIViewController *)currentViewController toViewController:(UIViewController *)nextViewController {

    CGFloat widthVC = self.containerView.frame.size.width;
    CGFloat heightVC = self.containerView.frame.size.height;

    if (currentViewController == nextViewController) return;

    if (_isNextViewController) {
        nextViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
        [self addChildViewController:nextViewController];
        [currentViewController willMoveToParentViewController:nil];

        [self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{

            nextViewController.view.frame = currentViewController.view.frame;
            currentViewController.view.frame = CGRectMake(0 - widthVC, 0, widthVC, heightVC);
        }
                                completion:^(BOOL finished) {
                                    [currentViewController removeFromParentViewController];
                                    [nextViewController didMoveToParentViewController:self];
                                }];
    }

    else {
        nextViewController.view.frame = CGRectMake(0 -widthVC, 0, widthVC, heightVC);
        [self addChildViewController:nextViewController];
        [currentViewController willMoveToParentViewController:nil];

        [self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{

            nextViewController.view.frame = currentViewController.view.frame;
            currentViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
        }
                                completion:^(BOOL finished) {
                                    [currentViewController removeFromParentViewController];
                                    [currentViewController didMoveToParentViewController:self];
                                }];
    }
}

Now I'm having a problem when I quickly push the forward and back buttons (the ones I use to present the new view controller or to go back to the previous view controller). My app crasha and consol returns this error to me :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Children view controllers and must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'

can someone help me understand where I'm wrong in my code?


Solution

  • It's possible that your currentViewController is not in yet fully added to self when you call the moveViewController:toViewController the second time around. You could place a statement like this in the beginning of your method to see if my assumption is correct:

    NSAssert([self.childViewControllers containsObject:currentViewController], @"The currentViewController: %@ is not a childViewController, here are the current childViewControllers: %@", currentViewController, self.childViewControllers);
    

    Edit: If this assumption isn't correct it would probably be helpful to edit your question to include your calls to this method for further context