18
votes

My application has a navigation controller and I don't want any animation in it :

  • to prevent an animation when pushing a view, it's easy, via the pushViewController:animated: method

  • but when I click the "back" button on this subview, there's an animation ! KO ! What can I do to prevent this animation ?

5
Why do you want to break the UI with non-standard behavior?Dave DeLong
It's not that I want to break the UI with non-standard behavior. I just don't understand why the SDK features control for a push to be animated or not but does not feature the same control for a pop. I feel it's inconsistent.Dirty Henry
non-standard behaviour makes us grow :)Zac

5 Answers

8
votes

This prevents default animation.

- (void)viewWillDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: NO];
}

- (void)viewDidDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: YES];
}

In case if you need a custom animation

- (void)viewWillDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: NO];

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = kCATransitionFade;
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
}

- (void)viewDidDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: YES];
}
3
votes

I came to SO looking for a more elegant solution, but here's how I've been (successfully) doing it so far.

The basic idea:

  1. DO NOT use UINavigationController; instead use it's constituent parts (e.g. UINavigationBar) and do the work yourself
  2. Trigger the navbar to animate in parallel with your own custom animations (or not, if you want no anim at all)

The downsides:

  1. UINavigationController handles some other things, like memory loading/unloading, automatically. Also, it's "hard coded" into all UIViewControllers - they ALWAYS have a reference to the UINavigationController that contains them. It's a shame to throw all this away just because Apple doesn't provide a hook for setting custom anims.

Code - in whichever class takes over for the animation:

UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[navigationController.navigationBar pushNavigationItem:backItem animated:TRUE];
// next line only needed if you want a custom back anim too
navigationController.navigationBar.delegate = self;

...if you also want to cut-in with custom back animation, you need that last line above, so that you can then listen to the navbar, and react in parallel, like this:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    // trigger your custom back animation here

    return TRUE;
}
3
votes

More elegant with a category. This assumes you navigation controller object is set in your app delegate. Just put this before your @implementaion in the root view controller.

#import "AppDelegate.h"

@implementation UINavigationBar (custom)
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
{

    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    [delegate.navController popViewControllerAnimated:NO];

    return TRUE;
}


@end
2
votes

Not that you should, however you can override the standard behaviour by creating a custom leftBarButtonItem in your viewController.

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[[self navigationItem] setLeftBarButtonItem:item];
[item release];

- (void)backButtonPressed
{
    [[self navigationContoller] popViewControllerAnimated:NO];
}

The documentation says that you should only pass NO before the nav controller's view is displayed.

Remember that applications that do not conform to the iPhone Interface Guidelines will not be accepted into the app store.

0
votes

I just answered another related question that describes how to easily create a custom back button that replicates the look of the standard iOS (iPhone / iPad) UI back bar button item but allows other functions to be added. As recommended in falconcreek's answer, in the backButtonPressed method, just add:

[[self navigationController] popViewControllerAnimated:NO];