Подтвердить что ты не робот

Просмотреть жизненный цикл контроллера на UIModalPresentationOverCurrentContext

Как определить, когда родительский контроллер просмотра был скрыт или показан, когда я использую стиль модальной презентации UIModalPresentationOverCurrentContext? В нормальных ситуациях я могу использовать viewWillAppear: и viewWillDisappear:, но они, похоже, не стреляют в это.

4b9b3361

Ответ 1

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