Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify direction of "show" (push) segue

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.

like image 813
Jake Chasan Avatar asked May 30 '15 22:05

Jake Chasan


1 Answers

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:

  • WWDC 2013 - Custom Transitions Using View Controllers

  • Animated Transitions in Swift / iOS8

  • Introduction to Custom View Controller Transitions and Animations

  • RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App

(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.

like image 145
ABakerSmith Avatar answered Oct 08 '22 17:10

ABakerSmith