如何正确刷新UINavigationBar? [英] How to properly refresh a UINavigationBar?
问题描述
关于此问题:如何在子视图控制器中更改后退按钮文本?我正在使用 previousViewController.navigationItem.backBarButtonItem?更改后退按钮标题之后,寻找一种刷新导航栏的适当方法。 title =新标题
。
In relation to this question: How to change Back button text from within the child view controller? I am searching for a propery way to refresh the navigation bar after changing the back button title with previousViewController.navigationItem.backBarButtonItem?.title = "New Title"
.
链接问题的解决方案(不太理想?):
The (not so ideal?) solution from the linked question:
if let navigationController = self.navigationController {
navigationController.popViewControllerAnimated(false)
navigationController.pushViewController(self, animated: false)
}
编辑:
显然更改图层框架会强制导航栏刷新。不是解决方案,而是一种更便宜的解决方法(我):
Apparently changing the layer frame forces the navigation bar to refresh. Not a solution, but a less expensive(?) workaround I guess:
if let navigationController = self.navigationController {
navigationController.navigationBar.layer.frame.insetInPlace(dx: 0.1, dy: 0)
navigationController.navigationBar.layer.frame.insetInPlace(dx: -0.1, dy: 0)
}
推荐答案
尝试各种刷新方法后,我发现这是最难看的似乎有效的解决方案(当时在iOS 10 上,但目前在iOS 13上显然不可用,即不要指望此):
After trying various methods to refresh, I find this is the least ugly solution that seems to work (back then on iOS 10 but apparently not currently on iOS 13, i.e., don't count on this):
guard let navigation = navigationController,
!(navigation.topViewController === self) else {
return
}
let bar = navigation.navigationBar
bar.setNeedsLayout()
bar.layoutIfNeeded()
bar.setNeedsDisplay()
尝试了其他方法:
- 呈现视图控制器(导致屏幕闪烁
- 隐藏并重新显示该条(如果在向后滑动到以前的VC之间有一半,则会中断条)
- 设置条的图层框架(似乎不能可靠地工作,并且
navigationBar
的文档明确禁止使用)
- Presenting a view controller (causes screen to flicker in some cases)
- Hiding and re-showing the bar (breaks bar if half-way between backswipe to previous VC)
- Setting the bar's layer's frame (does not seem to work reliably, and is explicitly forbidden by the documentation for
navigationBar
)
这篇关于如何正确刷新UINavigationBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!