Reversal of Custom Segue with Storyboarding

15,481

Solution 1

Yes. Here is an example where I pop to the top level. When your create the segue in Storyboard. Use select or enter the new new segue class in the attributes inspector.

//
//  FlipTopPop.h

#import <UIKit/UIKit.h>


@interface FlipTopPopToRoot : UIStoryboardSegue

@end

and

//  FlipTopPop.m

#import "FlipTopPopToRoot.h"

@implementation FlipTopPopToRoot

- (void) perform {

    UIViewController *src = (UIViewController *) self.sourceViewController;
    [UIView transitionWithView:src.navigationController.view duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromBottom
                animations:^{
                    [src.navigationController popToViewController:[src.navigationController.viewControllers objectAtIndex:0] animated:NO];;
                }
                    completion:NULL];
}

@end

If you want to pop up just one level change use this custom segue:

//  PopSegue.h

#import <UIKit/UIKit.h>

@interface PopSegue : UIStoryboardSegue

@end

and

//  PopSegue.m

#import "PopSegue.h"

@implementation PopSegue

- (void) perform {

    UIViewController *src = (UIViewController *) self.sourceViewController;
    [src.navigationController popViewControllerAnimated:YES];
}

@end

Solution 2

For anyone following this now, iOS 7 lets you animate both ways:

Set the segue to Push, then see code below for a push implementation.

https://github.com/Dzamir/OldStyleNavigationControllerAnimatedTransition

Share:
15,481

Related videos on Youtube

Jacob
Author by

Jacob

Updated on June 04, 2022

Comments

  • Jacob
    Jacob about 2 years

    I have a custom segue animation that occurs when pushing a new view controller onto the stack. When popping the view controller that was presented with said custom segue, however, the default navigation controller animation happens (that is, the current view controller animates to the right while the parent view controller translates on-screen from the left edge).

    So my question is this: is there a way to write a custom pop segue animation which happens when popping a view controller off the stack?

    Edit (solution):

    I ended up defining a custom segue similar to the selected answer. In the Storyboard, I dragged a custom segue from the child view controller back to its parent, gave it an identifier and the newly written reverse segue as its class. Yes, I realize it is virtually identical to a modal transition. Client requirements necessitated this madness, so before anyone comments, understand that I know one shouldn't have to do this under normal circumstances.

    - (void)perform {
      UIViewController *src = (UIViewController *)self.sourceViewController;
      UIViewController *dest = (UIViewController *)self.destinationViewController;
    
      [UIView animateWithDuration:0.3 animations:^{
        CGRect f = src.view.frame;
        f.origin.y = f.size.height;
        src.view.frame = f;
    
      } completion:^(BOOL finished){
        src.view.alpha = 0;
        [src.navigationController popViewControllerAnimated:NO];
      }];
    }
    
    • qix
      qix about 12 years
      By using a segue looping back to itself, aren't you still creating another instance of the original view controller unnecessarily? I guess no one would notice except for a brief blip in a memory profile. :)
  • Eric
    Eric almost 12 years
    PopSegue should be apart of UIKit. Good One!
  • mdupls
    mdupls almost 12 years
    This creates/deallocates a new destination view controller unnecessarily. It would be best to not use a Segue and instead just pop the view controller. Yes, I agree - Apple should include support for a pop segue.
  • ChavirA
    ChavirA almost 10 years
    I liked the solution for PopToRoot, but the one for pop just one view...not much. I added a little something so when you need to go back just one the animation still exists: - (void) perform { UIViewController *src = (UIViewController *) self.sourceViewController; [UIView transitionWithView:src.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [src.navigationController popViewControllerAnimated:NO]; } completion:NULL]; }