views:

875

answers:

5

I have a navigation based application and I want to change the animation of the push and pop animations. How would I do that?

A: 

I am not aware of any way you can change the transition animation publicly.

If the "back" button is not necessary you should use modal view controllers to have the "push from bottom" / "flip" / "fade" / (≥3.2)"page curl" transitions.


On the private side, the method -pushViewController:animated: calls the undocumented method -pushViewController:transition:forceImmediate:, so e.g. if you want a flip-from-left-to-right transition, you can use

[navCtrler pushViewController:ctrler transition:10 forceImmediate:NO];

You can't change the "pop" transition this way, however.

KennyTM
+3  A: 

Using private calls is a bad idea as Apple no longer approve apps that do that. Maybe you could try this:

//Init Animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.50];


[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:YES];

//Create ViewController
MyViewController *myVC = [[MyViewController alloc] initWith...];

[self.navigationController pushViewController:myVC animated:NO];
[myVC release];

//Start Animation
[UIView commitAnimations];
nicktmro
Are you sure this works?
Jaba
It only "half" works - it won't solve the harder problem of pop animations.
Adam
I like this solution better, and yes it works. Using private methods will get you rejected for sure.
Jami
A: 

nicktmro's answer works

Antony Clements
that's the purpose of the "upvote" button :)
Sam V
A: 

See my more detailed answer, using only public methods, here:

http://stackoverflow.com/questions/3119528/prevent-the-animation-when-clicking-back-button-in-a-navigation-bar/3127914#3127914

...it's imperfect (it means re-implementing some of UINavigationController) - but it doesn't use any private methods, and it works.

Adam
A: 

I found a mildly recursive way to do this that works for my purposes. I have an instance variable BOOL that I use to block the normal popping animation and substitute my own non-animated pop message. The variable is initially set to NO. When the back button is tapped, the delegate method sets it to YES and sends a new non-animated pop message to the nav bar, thereby calling the same delegate method again, this time with the variable set to YES. With the variable is set to YES, the delegate method sets it to NO and returns YES to allow the non-animated pop occur. After the second delegate call returns, we end up back in the first one, where NO is returned, blocking the original animated pop! It's actually not as messy as it sounds. My shouldPopItem method looks like this:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

if ([[navigationBar items] indexOfObject:item] == 1) {
    [expandedStack restack];    
}

if (!progPop) {
    progPop = YES;
    [navBar popNavigationItemAnimated:NO];
    return NO;
}

else {
    progPop = NO;
    return YES;
}

}

Works for me.

CharlieMezak