iosswiftuistoryboardsegueuipresentationcontroller

How to implement UIViewControllerTransitioningDelegate in UIStoryboardSegue?


This is what I do now:

func presentOverlayController(controller: UIViewController) {
    
    controller.modalPresentationStyle = .Custom
    controller.transitioningDelegate = self
    
    presentViewController(controller, animated: true, completion: nil)
}

//MARK: - UIViewControllerTransitioningDelegate

public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
    return OverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
}

It works pretty awesome. First I get the controller from storyboard using .instantiateViewControllerWithIdentifier:. Controller is presented the right way:

enter image description here

But the same result I need to achieve with my custom segue:

class OverlaySegue: UIStoryboardSegue, UIViewControllerTransitioningDelegate {

    override func perform() {
        
        destinationViewController.modalPresentationStyle = .Custom
        destinationViewController.transitioningDelegate = self
    }

    //MARK: - UIViewControllerTransitioningDelegate

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        return OverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
}

but it doesn't work. Why?


Solution

  • At the end of perform you need to call presentViewController(destinationViewController, animated: true, completion: nil).