0

I've got a UITabBarControllerDelegate set to itself for a UITabBarController. I just want to animate the transition for the UITabBarController child view controllers.

I'm using these methods from UITabBarControllerDelegate and UIViewControllerAnimatedTransitioning:

- (id<UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    return self;
}


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

    [container addSubview:toViewController.view];
    [container addSubview:fromViewController.view];

    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;

    if ([[toViewController valueForKey:@"title"] isEqualToString:@"viewControllerTitle"]) {
        [container bringSubviewToFront:toViewController.view];

        [fromViewController.view setFrame:CGRectMake(0, 0, 320, screenHeight)];
        [toViewController.view setFrame:CGRectMake(320, 0, 320, screenHeight)];

        [UIView animateWithDuration:0.3 animations:^{
            [toViewController.view setFrame:CGRectMake(0, 0, 320, screenHeight)];
        }
                         completion:^(BOOL finished){
                             [transitionContext completeTransition:finished];
                         }];
    }
}

Every lines of code above are being hit, but the result is just one black screen behind the UITabBar at the bottom, and if I remove the UITabBarControllerDelegate method to not call the transitioning context code, everything works fine.

I'm using a similar transitioning animation code for another part of my app that's just called from a UIViewController.transitioningDelegate instead of a UITabBarController.delegate and it works fine, so I think something is different about how UITabBarControllerDelegate handles transitions.

Can someone see why it gives me that black screen?

Thanks in advance!

JackyJohnson
  • 3,106
  • 3
  • 28
  • 35

1 Answers1

0

You could try something like that. There is also a question about that already answered: answer

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSArray *tabViewControllers = tabBarController.viewControllers;
    UIView * fromView = tabBarController.selectedViewController.view;
    UIView * toView = viewController.view;
    if (fromView == toView)
        return;
    NSUInteger fromIndex = [tabViewControllers indexOfObject:tabBarController.selectedViewController];
    NSUInteger toIndex = [tabViewControllers indexOfObject:viewController];

    [UIView transitionFromView:fromView
                        toView:toView
                      duration:0.3
                       options: UIViewAnimationOptionTransitionFlipFromRight
                    completion:^(BOOL finished) {
                        if (finished) {
                            tabBarController.selectedIndex = toIndex;
                        }
                    }];
    return true;
}
Community
  • 1
  • 1
user2432612
  • 188
  • 1
  • 7
  • I saw that one, but none of the answers provided are using the iOS7 transitions API, since it was introduced in Q3 of '13. I've started using it just recently so I'd like to understand why it's not working in this case. – JackyJohnson Jul 24 '14 at 11:21
  • @believesInSanta Have you tried with this one [link](http://www.objc.io/issue-5/view-controller-transitions.html) – user2432612 Jul 24 '14 at 11:26
  • Yeah, I'm basically doing what this article says, and it works, except when I do the same with a UITabBarController I get an empty view controller. I'm taking a break from it now. I think the destination view controller might not be passed right? – JackyJohnson Jul 24 '14 at 12:15