AnthonyR AnthonyR - 1 year ago 127
Swift Question

Delegate methods are not called (transitioningDelegate)

I'm trying to implement a custom transition between two viewControllers. (Swift 3.0)

Between my two viewControllers I have a

with the kind
animated = true

So I set the delegate methods of
in the extension of my first view controller :

extension DocumentsViewController : UIViewControllerTransitioningDelegate { ... }

And I also have implemented the required methods by the protocol :

animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

Now when the
is perform, in the
I'm using the delegate method
to finally set the
to my `secondViewController, see below :

public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let destination = segue.destination as? DocumentDetailViewController {
destination.transitioningDelegate = self

I check with breakpoints, the delegate is well setted to my firstViewController.
But the delegate methods of
in my firstViewController are never fired, I don't know why.

Any ideas ?

PS : In my storyboard, my
segue have Animated to true
, so this should work, but it doesn't.


SOLVED : A mix of MadreDeDios, Matt and Tushar answers.

1 : As I want to keep the navigation in my app, I have to make conform my first viewController to
instead of
. (see MadreDeDios answer's).

2 : According to this protocol, I have implemented the following delegate method :

public func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {



3 : I set the delegate earlier at the
of my
(see Matt's answer) :

override public func viewDidLoad() {
self.navigationController?.delegate = self

4 : I'm using a manual push instead of a segue to display my
(see Tushar's answer).

Now this works, thank you.

Answer Source

The problem is that you are setting the transitioningDelegate too late. Way too late. It needs to be set very early in the lifetime of the view controller. I advise setting this property in the view controller's initializer.