Search code examples
uinavigationcontrollerswift2uistoryboardseguepushviewcontrollerpresentviewcontroller

NavigationController.pushViewController in Custom Segue does not work - Swift 2.0 - iOS 9.1


I'm writing a custom segue in UIStoryboardSegue. I use a CATransition to create a right to left custom animation. If I used sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false);, it does not take me to the second view. It just performs the right to left animation and stays on the first view. I have debugged this code, and I found that sourceViewController.navigationController is nil. I have a UINavigationController between the first view and the second view.

I have also tried to replace this line of code with sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil);. The transition does happen, but it happens too fast that I can't see the right to left animation.

My complete code in this UIStoryboardSegue is the following:

override func perform() {


    let sourceViewController = self.sourceViewController as UIViewController;
    let destinationViewController = self.destinationViewController as UIViewController;
    let transition: CATransition = CATransition()

    transition.duration = 0.25;
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;

    sourceViewController.view.layer.addAnimation(transition, forKey: "kCATransition")

    sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false);
}

Thank you very much for your help


Solution

  • Using sourceViewController.pushViewController is correct. The reason that I was getting nil for the sourceViewController.navigationController was that, I placed the navigation controller between the first view and the second view. The correct way of placing the navigation controller is to place it before the first view.

    This is the correct way of placing the navigation controller: