Changing the UINavigationController animation style

Posted by David Hamrick on


I was working on project recently where I had the requirement to change between two sections of an application by fading between them. Since I needed to be able to go back to the first section from second section it was a prime candidate for a UINavigationController. I also wanted to have the ability to unload the first section if we were in a low memory environment (something that happens automatically with UINavigationController)

Changing the style of the animation turned out to be quite simple. All you need to do is disable animation on the pushViewController:animated: method and attach your own CAAnimation.

You can pop using the same method by replacing the last method with popViewControllerAnimated:

This demo video shows this trick switching back and forth between two view controllers.

This little trick saved a ton of time re-implenting UINavigationController just to change the animation style. Since I started using it in multiple places I ended up putting it in a category.

- (void)pushFadeViewController:(UIViewController *)viewController;
- (void)fadePopViewController;

@end

- (void)pushFadeViewController:(UIViewController *)viewController
{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.view.layer addAnimation:transition forKey:nil];

    [self pushViewController:viewController animated:NO];
}

- (void)fadePopViewController
{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.view.layer addAnimation:transition forKey:nil];
    [self popViewControllerAnimated:NO];
}

@end



About.

David Hamrick is a partner at Hamrick Software, the makers of VueScan, the worlds most popular 3rd party scanner software.