Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Controller lifecycle on UIModalPresentationOverCurrentContext

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.

like image 962
Mikko Harju Avatar asked May 27 '15 05:05

Mikko Harju


1 Answers

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.

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.

Here is an example implementation,

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()
    }
}
like image 128
Sandeep Avatar answered Sep 23 '22 18:09

Sandeep