关于这个问题:How to change Back button text from within the child view controller?在使用 previousViewController.navigationItem.backBarButtonItem?.title = "New Title"
更改后退按钮标题后,我正在寻找一种刷新导航栏的正确方法。
来自链接问题的(不太理想?)解决方案:
if let navigationController = self.navigationController {
navigationController.popViewControllerAnimated(false)
navigationController.pushViewController(self, animated: false)
}
编辑:
显然,更改图层框架会强制刷新导航栏。我猜这不是解决方案,而是更便宜(?)的解决方法:
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,即不要指望这个):
guard let navigation = navigationController,
!(navigation.topViewController === self) else {
return
}
let bar = navigation.navigationBar
bar.setNeedsLayout()
bar.layoutIfNeeded()
bar.setNeedsDisplay()
尝试过的其他方法:
- 呈现一个 View Controller (在某些情况下会导致屏幕闪烁)
- 隐藏和重新显示栏(如果在向后滑动到上一个 VC 之间的中途中断栏)
- 设置栏层的框架(似乎不能可靠地工作,并且被
navigationBar
的文档明确禁止)
关于ios - 如何正确刷新 UINavigationBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39066126/