UINavigationController: pop view controller in the opposite direction
Asked Answered
S

3

6

I'm trying to call [[self navigationController] popViewControllerAnimated:YES] but making the animation slide right to left instead of left to right. Any easy way to do this? I want to return to the previous view. Any help is appreciated. Thanks!

Solenoid answered 6/11, 2011 at 22:32 Comment(0)
D
10

It is possible, look at the following code I used a while back and try to make it work for yourself. Yu only need to change the setAnimationTransition

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.375];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];

There are a few different kind of default animations to use, apples site says this kind of animations are possible:

typedef enum {
   UIViewAnimationTransitionNone,
   UIViewAnimationTransitionFlipFromLeft,
   UIViewAnimationTransitionFlipFromRight,
   UIViewAnimationTransitionCurlUp,
   UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;

So in your case you would want to use the following:

    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
Dynatron answered 6/11, 2011 at 22:41 Comment(8)
Thanks. Is there a way to slide it as if you were calling pushViewController?Solenoid
Yes like i said, you only need to change the line here: [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];Dynatron
I understand that. I can't find the correct transition though.Solenoid
It's just that none of those do what I want them to do. I want it to have exactly the same motion as if the UINavigationController was pushing a new view controllerSolenoid
It does, i will edit my post to make this animation, this will be exactly what you want.Dynatron
Your solution does something weird. It pops the view without any animation but only the title on the Navigation Bar slides in, not the view itself. Thoughts?Solenoid
I don't know what your code is, but basically what i did there is exactly what you want to use, try to edit it to your own code.Dynatron
Did not. That's okay, though, I ended up going with a different UI strategy. Thanks for the help! Will accept your answer so you can get the points.Solenoid
C
18

This is how one can pop view controller in opposite direction. Its working for me 100%

CATransition *transition = [CATransition animation];
    transition.duration = 0.45;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    transition.type = kCATransitionFromRight;
    [transition setType:kCATransitionPush];
    transition.subtype = kCATransitionFromRight;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:NO];
Candycandyce answered 22/4, 2013 at 11:5 Comment(1)
You set the type twice? transition.type = kCATransitionPush should be enough.Ebro
D
10

It is possible, look at the following code I used a while back and try to make it work for yourself. Yu only need to change the setAnimationTransition

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.375];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];

There are a few different kind of default animations to use, apples site says this kind of animations are possible:

typedef enum {
   UIViewAnimationTransitionNone,
   UIViewAnimationTransitionFlipFromLeft,
   UIViewAnimationTransitionFlipFromRight,
   UIViewAnimationTransitionCurlUp,
   UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;

So in your case you would want to use the following:

    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
Dynatron answered 6/11, 2011 at 22:41 Comment(8)
Thanks. Is there a way to slide it as if you were calling pushViewController?Solenoid
Yes like i said, you only need to change the line here: [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];Dynatron
I understand that. I can't find the correct transition though.Solenoid
It's just that none of those do what I want them to do. I want it to have exactly the same motion as if the UINavigationController was pushing a new view controllerSolenoid
It does, i will edit my post to make this animation, this will be exactly what you want.Dynatron
Your solution does something weird. It pops the view without any animation but only the title on the Navigation Bar slides in, not the view itself. Thoughts?Solenoid
I don't know what your code is, but basically what i did there is exactly what you want to use, try to edit it to your own code.Dynatron
Did not. That's okay, though, I ended up going with a different UI strategy. Thanks for the help! Will accept your answer so you can get the points.Solenoid
A
0

Swift version:

    let animation = CATransition()
    animation.duration = 0.3
    animation.type = .push
    animation.subtype = .fromRight
    animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
    navigationController?.view.layer.add(animation, forKey: nil)
    navigationController?.popViewController(animated: false)
Atterbury answered 27/9, 2023 at 10:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.