Question

I need to run a method after a tabBar button is pressed, but BEFORE the view transition. And to run a second method AFTER the view transition. The goal is to add exit/enter effects.

I tried to use

ViewDidAppear 

and

ViewWillDisapear

but it didn't work.

Also,

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController

doesn't seem to allow an animation to perform before/after the view is switched.

Does anyone has an idea on how to do this?

Thanks in advance.

Was it helpful?

Solution

Try this, and see if it works. For the before animation, implement tabBarController:shouldSelectViewController:, and put your animation code there. Return YES in the completion block of the animation so the switch to the new tab will be delayed until your animation is over. The after animation should be put in the viewDidAppear method of the tab you're moving to.

After Edit:

It's a little more complicated than my answer above, so it has to be done by calling a method in tabBarController:shouldSelectViewController: that does the animation, then selects the view controller to move to -- by doing the selection in code, the delegate method won't be called for a second time. Here's an example where I just move a view in the animation block,

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    [self completeTabChangeToController:viewController];
    return NO;
}


-(void)completeTabChangeToController:(UIViewController *) controller {
    self.bottomCon.constant = 50;
    [UIView animateWithDuration:.5 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.tabBarController setSelectedViewController:controller];
    }];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top