在 UIModalPresentationOverCurrentContext 上查看控制器生命周期

View Controller lifecycle on UIModalPresentationOverCurrentContext

当我使用 UIModalPresentationOverCurrentContext 模态呈现样式时,如何确定父视图控制器何时隐藏或显示?在正常情况下,我可以使用 viewWillAppear:viewWillDisappear:,但他们似乎并没有为此开火。

UIModalPresentationOverCurrentContext 旨在用于显示当前 viewController 上的内容。这意味着,如果您的 parentViewController 中有动画或视图更改,如果视图是透明的,您仍然可以透过 childViewController 看到。因此,这也意味着视图永远不会消失以查看当前上下文。 viewWillAppear:、viewDidAppear:、viewWillDisappear: 和 viewDidDisappear 没有被调用似乎是合法的。

但是,您可以使用 UIModalPresentationStyle.Custom 来在当前上下文中呈现完全相同的行为。您不会收到视图外观回调,但您可以创建自己的自定义 UIPresentationController 来获取这些回调。

这是一个示例实现,

class MyFirstViewController: UIViewController {

            ....

    func presentNextViewController() {
        let myNextViewController = MyNextViewController()

        myNextViewController.modalPresentationStyle = UIModalPresentationStyle.Custom
        myNextViewController.transitioningDelegate = self
        presentViewController(myNextViewController, animated: true) { _ in

        }
    }

               ...
}

extension MyFirstViewController: UIViewControllerTransitioningDelegate {

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let customPresentationController = MyCustomPresentationController(presentedViewController: presented, presentingViewController: presenting)
        customPresentationController.appearanceDelegate = self
        return customPresentationController
    }
}

extension MyFirstViewController: MyCustomApprearanceDelegate {

    func customPresentationTransitionWillBegin() {
        print("presentationWillBegin")
    }

    func customPresentationTransitionDidEnd() {
        print("presentationDidEnd")
    }

    func customPresentationDismissalWillBegin() {
        print("dismissalWillBegin")
    }

    func customPresentationDismissalDidEnd() {
        print("dismissalDidEnd")
    }
}




protocol MyCustomApprearanceDelegate: class {
    func customPresentationTransitionWillBegin()
    func customPresentationTransitionDidEnd()
    func customPresentationDismissalWillBegin()
    func customPresentationDismissalDidEnd()
}

class MyCustomPresentationController: UIPresentationController {

    weak var appearanceDelegate: MyCustomApprearanceDelegate!

    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }

    override func presentationTransitionWillBegin() {
        appearanceDelegate.customPresentationTransitionWillBegin()
    }

    override func presentationTransitionDidEnd(completed: Bool) {
        appearanceDelegate.customPresentationTransitionDidEnd()
    }

    override func dismissalTransitionWillBegin() {
        appearanceDelegate.customPresentationDismissalWillBegin()
    }

    override func dismissalTransitionDidEnd(completed: Bool) {
        appearanceDelegate.customPresentationDismissalDidEnd()
    }
}