在UIModalPresentationOverCurrentContext上查看控制器生命周期 [英] View Controller lifecycle on UIModalPresentationOverCurrentContext

查看:383
本文介绍了在UIModalPresentationOverCurrentContext上查看控制器生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 UIModalPresentationOverCurrentContext 模式演示样式时,如何确定隐藏或显示父视图控制器的时间?在正常情况下,我可以使用 viewWillAppear: viewWillDisappear:,但他们似乎没有对此进行解雇。

How do I determine when the parent view controller has been hidden or shown when I use the UIModalPresentationOverCurrentContext modal presentation style? On normal situations I can use the viewWillAppear: and viewWillDisappear:, but they seem not to be firing on this.

推荐答案

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

UIModalPresentationOverCurrentContext is intended to be used to present the content over your current viewController. What that means is, if you have animation or view changes inside your parentViewController, you can still see through the childViewController if the view is transparent. So, it also means that view never disappears for view over current context. It seems legit that viewWillAppear:, viewDidAppear:, viewWillDisappear: and viewDidDisappear do not get called.

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

You can however use UIModalPresentationStyle.Custom to have exact same behavior to present over current context. You wont receive view appearance callbacks but you can create your own custom UIPresentationController to get those callbacks.

这是一个示例实现,

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 {
    func customPresentationTransitionWillBegin()
    func customPresentationTransitionDidEnd()
    func customPresentationDismissalWillBegin()
    func customPresentationDismissalDidEnd()
}

class MyCustomPresentationController: UIPresentationController {

    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()
    }
}

这篇关于在UIModalPresentationOverCurrentContext上查看控制器生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆