Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...