Custom ViewController Transition 遇到的一个坑

1. Custom ViewControllerTransition

In UIViewControllerTransitioningDelegate, apple says:

An object that implements the UIViewControllerTransitioningDelegate protocol vends the objects used to manage a fixed-length or interactive transition between view controllers. When you want to present a view controller using a custom modal presentation type, set its modalPresentationStyle property to UIModalPresentationCustom and assign an object that conforms to this protocol to its transitioningDelegate property. When you present that view controller, UIKit queries your transitioning delegate for the objects to use when animating the view controller into position.

Shortly, you set modalPresentationStyle to ** UIModalPresentationCustom** and set the transitioningDelegate, likes:
presentedVC.modalPresentationStyle = UIModalPresentationStyle.Custom presentedVC.transitioningDelegate = self presentViewController(presentedVC, animated: true, completion: nil)

The problem is when you dismiss the presentedVC, in your animate object (which conforms to UIViewControllerAnimatedTransitioning delegate) you get your toView with nil :
toView = transitionContext.viewForKey(UITransitionContextToViewKey)
if you forget checking nil before use it your app crash! Even if you checked, you still unable to animate your dismiss because toView is nil.


Solution:
just commit it :
//presentedVC.modalPresentationStyle = UIModalPresentationStyle.Custom presentedVC.transitioningDelegate = self presentViewController(presentedVC, animated: true, completion: nil)
then it work right!

你可能感兴趣的:(Custom ViewController Transition 遇到的一个坑)