Custom transition animation not calling VC lifecycle methods on dismiss

后端 未结 6 1518
梦谈多话
梦谈多话 2020-12-24 11:11

So I built a custom presenting transition animation and everything seems to be working great except the view controller lifecycle methods are not being called on dismiss.

6条回答
  •  無奈伤痛
    2020-12-24 11:52

    @John Tracids' anser solved my issue. Thanks John!

    But I would like to extend an answer a bit.

    If you are presenting UIViewController instance with modalPresentationStyle = .custom (objc UIModalPresentationCustom) in order to keep viewcontroller's lifecycle methods being called, you have to manage viewcontroller’s appearance explicitly. To do that just call beginAppearanceTransition before animation and endAppearanceTransition at the animation completion block.

    Also you can pass to your transitioning animator class custom UIPresentationController subclass with overridden value shouldRemovePresentersView returning true without calling beginAppearanceTransition

    // Swift 4

    put this to your custom UIViewControllerAnimatedTransitioning class before animation

    fromViewController.beginAppearanceTransition(false, animated: true)
    toViewController.beginAppearanceTransition(true, animated: true)
    
    UIView.animate(withDuration: animationDuration, animations: {
            // animation logic…
        }) { finished in
            fromViewController.endAppearanceTransition()
            toViewController.endAppearanceTransition()
            let transitionSuccess = !transitionContext.transitionWasCancelled
            transitionContext.completeTransition(transitionSuccess)
        }
    
    // UIPresentationController subclass
    class PresentationController: UIPresentationController {
    override var shouldRemovePresentersView: Bool { return true }
    }
    

提交回复
热议问题