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 ?
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];
}
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
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:
The downsides:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With