2

I am trying to implement a custom popup with custom transitions, but my delegate methods are not being called at all. This is my transitioning delegate:

public final class ModalTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {

    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let controller = ModalPresentationController(presentedViewController: presented, presenting: presenting)

        return controller
    }

    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ModalAnimationPresenter()
    }

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

This is my popup view controller:

class StopWorkoutViewController: UIViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        commonInit()
    }

    func commonInit() {

        let transitioner = ModalTransitioningDelegate()
        modalPresentationStyle = .custom
        transitioningDelegate = transitioner
    }
}

This is how I present the popup:

@IBAction func test(_ sender: Any) {
    let popup = UIStoryboard(name: "Popups", bundle: nil).instantiateInitialViewController() as! StopWorkoutViewController
    present(popup, animated: true, completion: nil)
}

And this is the view controller in IB:

Popup ViewController

The popup is presented, but full-screen.

smeshko
  • 1,184
  • 13
  • 27

3 Answers3

4

This is wrong

 func commonInit() {

        let transitioner = ModalTransitioningDelegate()
        modalPresentationStyle = .custom
        transitioningDelegate = transitioner
    }

Since you are going to animate the transition of StopWorkoutViewController.You need to set the transition delegate as

 @IBAction func test(_ sender: Any) {
    let transitioner = ModalTransitioningDelegate()
    let popup = UIStoryboard(name: "Popups", bundle: nil).instantiateInitialViewController() as! StopWorkoutViewController
    popup.transitioningDelegate = transitioner
    present(popup, animated: true, completion: nil)
}
LC 웃
  • 18,888
  • 9
  • 57
  • 72
  • I make this mistake every time. You have to set the transitioningDelegate on the view controller being presented, not the current view controller that's doing the presenting. – user3344977 Apr 30 '19 at 16:07
3

The issue is that transitioningDelegate is a weak property, so the class you assign to it is getting released before the transition has a chance to run. See my answer here for an example of how to catch this in the debugger.

Community
  • 1
  • 1
bcattle
  • 12,115
  • 6
  • 62
  • 82
0

In my case it was about present(_:animated:) and dismiss(animated:completion:) methods called with animated set to false

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93