如果点击后退按钮,则无法更改导航栏中标题的颜色。
有一个 UINavigationController。
它的rootViewController
是VC1。导航栏中标题的颜色设置为红色。
VC1 插入 VC2。导航栏中标题的颜色设置为绿色。
当VC2弹出时,导航栏中的标题颜色设置为红色。
如果使用后退按钮弹出VC2,导航栏的标题不会变为红色,仍保持绿色。
如果使用向后滑动手势弹出 VC2,导航栏的标题将按预期更改为红色。
导航 Controller 是它自己的委托(delegate)。
使用 navigationController(_:willShow:animated)
方法设置导航栏中图 block 的颜色。
extension NavigationController: UINavigationControllerDelegate {
func navigationController(
_ navigationController: UINavigationController,
willShow viewController: UIViewController,
animated: Bool
) {
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: viewController is VC1 ? UIColor.red : UIColor.green
]
navigationBar.titleTextAttributes = navigationBar.titleTextAttributes?.merging(attributes) { $1 }
}
}
预期 - 弹出 VC2 后,导航栏中的标题颜色应变为红色。
实际- VC2弹出后,导航栏标题颜色保持绿色。
谁能指出如何解决这个问题,使VC2弹出后导航栏中的标题颜色变成红色?
最佳答案
在 ViewDidLoad() 中执行此操作
let image = UIImage(named: "back")
self.navigationController?.navigationBar.backIndicatorImage = image
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = image
self.navigationItem.title = "Servicos"
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
关于ios - 如果点击后退按钮,导航栏标题颜色不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343169/