Specify direction of "show" (push) segue

10,028

I don't believe there is a built in method for achieve that, however you could use Custom Transitions, which were introduced in iOS7. There are plenty of great tutorials out there, I've added some links below and some sample code demonstrating how you could slide the new view controller down from the top.

Sites to check out:

(Note: the way a UIViewController's view is accessed in the third and fourth tutorials is outdated. Instead of toViewController.view you should use transitionContext.viewForKey(UITransitionContextToViewKey))

An example:

Firstly, you're going to need to set up your storyboard, which I'm going to assume you've already done.

Secondly, you need an NSObject subclass that conforms to UIViewControllerAnimatedTransitioning. This will be used to control the transition from one view controller to the next.

class TransitionManager : NSObject, UIViewControllerAnimatedTransitioning {
    let animationDuration = 0.5

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return animationDuration
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        containerView.addSubview(toView)

        let finalFrame  = containerView.bounds
        let initalFrame = CGRect(x: 0, y: -finalFrame.height, width: finalFrame.width, height: finalFrame.height)

        toView.frame = initalFrame
        UIView.animateWithDuration(animationDuration,
            animations: { toView.frame = finalFrame },
            completion: { _ in transitionContext.completeTransition(true) })
    }
}

As described in the RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App tutorial, you'll need to setup a your UINavigationController's delegate. The following NavigationControllerDelegate class will be used to return an instance of TransitionManager when a push segue is being conducted:

class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
    let transitionManager = TransitionManager()

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .Push {
            return transitionManager
        }

        return nil
    }
}

And that should be it! To expand on this I'd recommend you take a look at Interactive Transitions.

Share:
10,028
Jake Chasan
Author by

Jake Chasan

Jake Chasan is an Investment Banker at Goldman Sachs in New York. In his free time, he writes code to help entrepreneurs, small businesses, and volunteer organizations succeed. Proficient at C, C++, Objective-C, Swift, Java, PHP, MySQL, HTML, CSS, JavaScript, Assembly. He holds a certification from Oracle. Please go to jakechasan.com to find out more.

Updated on June 11, 2022

Comments

  • Jake Chasan
    Jake Chasan almost 2 years

    Is there a simple way to specify the direction (from left, right, top, or bottom) of a "show" segue (previously called a "push" segue)?

    The default "show" segue goes from left to right, but I would like it to go from bottom to top.