Specify the direction of the "show" (push) segue

Is there an easy way to indicate the direction (left, right, top, or bottom) of a "show" segue (formerly called a "push" segue)?

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

+7
ios objective-c swift segue
source share
1 answer

I do not believe that there is a built-in method for this, however you can use custom transitions that were introduced in iOS7. There are a lot of great tutorials out there, I added some links below and some sample code demonstrating how you can shift the new view controller on top.

Sites for registration:

(Note: access to the UIViewController view in the third and fourth tutorials is deprecated. Instead of toViewController.view you should use transitionContext.viewForKey(UITransitionContextToViewKey) )

Example:

First, you will need to set up a storyboard, which I’m going to suggest that you have already done.

Secondly, you need a subclass of NSObject that matches UIViewControllerAnimatedTransitioning . This will be used to control the transition from one controller to another.

 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 RayWenderlich: how to make a transition to view animation as in the Ping App , you need to configure your UINavigationController delegate. The following NavigationControllerDelegate class will be used to return the TransitionManager instance when executing the push segment:

 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 it must be! To expand on this, I would recommend you take a look at interactive transitions.

+7
source share

All Articles