views:

787

answers:

3

Hi,

As everyone know the UINavigationController push a ViewController from Left To Right, is there a way to push the View from Right To Left? like the animation for the back button.
For now I have this:


[self.navigationController pushViewController:viewController animated:YES];
+1  A: 

No, right to left is reserved for popping viewcontrollers from the navigation stack. You can however get such an effect by animating the views yourself. Something like:

[UIView beginAnimations:@"rightToLeft" context:NULL];
CGRect newFrame = aView.frame;
newFrame.origin.x -= newFrame.size.width;
aView.frame = newFrame;
[UIView commitAnimations];

This will however not do anything to your navigation controller.

Johan Kool
good to know, and how to do it if I pop the View From the navigation Stack?
ludo
@Ludo: you can't pop views from the navigation stack. the "stack" contains viewcontrollers
Felix
Sorry, my bad. I typed views, but of course had viewcontrollers in mind. How to pop a viewcontroller is explained by the other answers.
Johan Kool
I understand it don't worry
ludo
+2  A: 

You seem to want to "pop back". This can be achieved by three methods on your UINavigationController instance:

To come back to the "root" controller:

-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated

Or to come back to one of the previous controllers :

-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

Or to come back to the previously pushed controller :

-(UIViewController *)popViewControllerAnimated:(BOOL)animated

yonel
thanks that help, I just wanna try the Felix's answer but don't really know how to do it
ludo
+5  A: 

You can create a NSMutableArray from the navigationController's array of viewcontrollers and insert new viewController before the current one. Then set the viewControllers array without animation and pop back.

UIViewController *newVC = ...;
NSMutableArray *vcs =  [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:newVC atIndex:[vsc count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[self.navigationController popViewControllerAnimated:YES];
Felix
Interesting approach. Could work, though likely to come with some headscratching.
Johan Kool
sound kind of interesting. I need to pass somes informations inside the ViewController so maybe it can work with this way.Can you show me how I can insert my new ViewController before the current one?
ludo
I have added a code snippet, illustrating this approach. This is definitely not in line with the HIG and will likely confuse users. But it should do what you requested. I didn't test that though.
Felix
thats perfect, just need to change [vsc indexOfObject:self] by this one [vsc count] -1
ludo
that is more general you are right. edited my post.
Felix