我在根控制器上附加了一个导航控制器。在此控制器的viewWillAppear()方法内部,我总是展示一个带有自定义过渡的模式(它基本上是一个自定义警报)。首次打开该应用程序时,它始终有效,但是如果您关闭此警报,则将另一个控制器推到导航堆栈中,然后将其弹出。触发根控制器viewWillAppear时,无需进行自定义转换即可使用当前控件。
根控制器viewWillAppear:
let overlay = OverlayTransitioningDelegate()
let firstDebtController = self.storyboard?.instantiateViewController(withIdentifier: "firstDebtDialog") as! FirstDebtDialogController
firstDebtController.transitioningDelegate = overlay
firstDebtController.modalPresentationStyle = .custom
self.present(firstDebtController, animated: true, completion: nil)
由于它总是在第一次运行,因此我假设我的自定义过渡控制器无法正常工作?
委托:
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return OverlayPresentationController(presentedViewController:presented, presenting:presenting)
}
我添加的TransitionController方法:
override func dismissalTransitionDidEnd(_ completed: Bool) {
self.dimmedBackgroundView.removeFromSuperview()
}
@objc private func backgroundTapped() {
self.presentedViewController.dismiss(animated: false,
completion:
nil)
}
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped))
self.dimmedBackgroundView.addGestureRecognizer(tapGestureRecognizer)
}
正如我所说,当使用pop / popToRootController触发viewWillAppear时,创建的TransitionController甚至不会调用init()方法,也不调用presentationTransitionWillBegin(),例如,我认为它可能与根cv生命周期有关,但方法正在运行...
更新:我在DispatchQueue.main.asyncAfter中测试了以下代码,并且该代码正常工作。这是否意味着在弹出后触发根控制器viewWillAppear时,UI线程是“还没准备好吗?或者也许是在第一次使用过渡之后,有些东西迷路了?