gpt4 book ai didi

ios - 在 UIModalPresentationOverCurrentContext 上查看 Controller 生命周期

转载 作者:技术小花猫 更新时间:2023-10-29 10:49:37 27 4
gpt4 key购买 nike

当我使用 UIModalPresentationOverCurrentContext 模式呈现样式时,如何确定父 View Controller 何时隐藏或显示?在正常情况下,我可以使用 viewWillAppear:viewWillDisappear:,但它们似乎并没有触发。

最佳答案

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

但是,您可以使用 UIModalPresentationStyle.Custom 以完全相同的行为在当前上下文中呈现。您不会收到 View 外观回调,但您可以创建自己的自定义 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()
}
}

关于ios - 在 UIModalPresentationOverCurrentContext 上查看 Controller 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474034/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com