简体   繁体   中英

UIViewControllerTransitioningDelegate being deinitialized immediately after it is presented

I'm trying to present a view controller modally with a custom presenter using UIPresentationController (and UIViewControllerTransitioningDelegate ).

The problem is that the transitioning delegate is being deinitialized immediately after animationController(presented:presenting:source:) is called. This means animationController(dismissed:) never gets called - and thus, a dismissal animation cannot be set.

In the end, I want to be able to define the dismissal animation. I believe what I explained above is the root of the problem, but can't find anything about this online.


Here is my implementation of UIViewControllerTransitioningDelegate :

final class Manager: NSObject, UIViewControllerTransitioningDelegate {
    private let size: CGSize
    var animator: Animator

    init(size: CGSize) {
        self.size = size
        self.animator = Animator(duration: 0.4, loaf: loaf)
    }

    deinit {
        print("DEINIT") // 2) Then this is being called immediately after
    }

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return Controller(
            presentedViewController: presented,
            presenting: presenting,
            size: size
        )
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        animator.presenting = true
        return animator // 1) This is called first after the view controller is presented
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        animator.presenting = false
        return animator // 3) This is never called
    }
}

And this is how I'm setting the transitioning delegate:

extension UIViewController {
    func presentModally(_ viewController: UIViewController, size: CGSize) {
        viewController.transitioningDelegate = Manager(size: size)
        viewController.modalPresentationStyle = .custom
        present(viewController, animated: true)
    }
}

When the view controller is then dismissed, the view always defaults to being pushed down and disappearing. Again, animationController(dismissed:) is never called and I can't figure out why.

I was able to fix this by storing a reference to the UIViewControllerTransitioningDelegate on the presenting view controller. Then, when presenting the modal, set it like this:

extension UIViewController {
    func presentModally(_ viewController: UIViewController, size: CGSize) {
        viewController.transDelegate = Manager(size: size)
        viewController.transitioningDelegate = viewController.transDelegate
        viewController.modalPresentationStyle = .custom
        present(viewController, animated: true)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM