比方说,我有 3 个 View Controller :A
, B
, C
,全部嵌入到导航 Controller 中。 A
和 B
有一个导航栏,C
没有。
我在 B
之间有一个自定义交互转换和 C
.因为我需要我的导航栏在 C
上消失,我实现了UINavigationControllerDelegate
的这个功能:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController is C {
navigationController.setNavigationBarHidden(true, animated: animated)
}
else {
navigationController.setNavigationBarHidden(false, animated: animated)
}
}
当我只进行 push-pop 转换时,在常见场景中一切都完美运行。
但是当我取消转换时B
-> C
调用cancel()
在我的 UIPercentDrivenInteractiveTransition
,导航栏未显示在 B
上.在这里我必须打电话setNavigationBarHidden(false, ...)
,但我还没有设法找到正确的位置。
如果我在 B
中调用它的 viewWillAppear
, 导航栏出现,但看起来真的很奇怪 - 它包含 C
的元素如果它有导航栏就会有。如果我跳回到 A
, 它会以预期的内容闪烁片刻,但在转换后立即 A
导航栏被 B
取代导航栏!
因此,导航栏堆栈似乎在 B
之后以某种方式被破坏了。 -> C
转换取消,它似乎相对于 viewcontrollers 移动了这样的:
has
-----------------------------------------------
| ViewController | Navigation bar of |
-----------------------------------------------
| A | B |
-----------------------------------------------
| B | C |
-----------------------------------------------
所以,我的问题是调用 navigationController.setNavigationBarHidden(false, animated: true)
的正确位置是什么?在这种情况下?
最佳答案
好吧,我设法找到了一个丑陋的 hack 来自己修复它。也许这个世界上有人会觉得它有帮助。
在我的自定义
UIPercentDrivenInteractiveTransition
中,我重写了cancel
函数:class CustomTransitionManager: UIPercentDrivenInteractiveTransition { /// Indicates transition direction. Must be set before each transition. var forward: Bool = true /// Current navigation controller used for transition. Must be set before transition starts var nc: UINavigationController? /** * Hack #1 with UINavigationController here */ override func cancel() { super.cancel() if forward { self.nc?.setNavigationBarHidden(false, animated: false) } self.nc?.setNavigationBarHidden(forward, animated: false) } }
在每个 View Controller (A、B、C)中,我进行以下修改:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Hide and immediately show navigation bar: this will restore it's correct state self.navigationController?.setNavigationBarHidden(true, animated: false) self.navigationController?.setNavigationBarHidden(false, animated: true) }
最好的解决方案可能是 C
的模态全屏显示,但就我而言,我正在处理一个导航层次结构已经损坏的项目,我没有时间正确修复它。基本上,这就是我遇到这个问题的原因。
关于ios - 交互式转换 : navigation bar appearance issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892580/