自从更新到XCode 11.4以来,我遇到了一个问题,我不知道如何在导航栏中更改后退箭头的颜色。
在我能够简单使用之前:
navigationBar.barTintColor = DisplayUtils.sweetGreenColor()
但是,11.4迫使我使用standardAppearance功能来设置导航栏和所有内容的颜色。没什么大不了的,这适用于设置barTint和标题颜色:
navigationBar.standardAppearance.backgroundColor = DisplayUtils.sweetGreenColor()
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [.foregroundColor : DisplayUtils.whiteColor()]
navigationBar.standardAppearance.buttonAppearance = buttonAppearance
navigationBar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: DisplayUtils.whiteColor(), NSAttributedString.Key.font: UIFont(name: "Roboto-Bold", size: 18) as Any]
我只是没有在UIBarButtonItemAppearance对象上看到替换barTintColor的属性。它使您可以更改文本的颜色,更改图像本身,但没有color属性。
这应该是一个如此简单的任务,我很犹豫要发布此问题...
提前感谢。
我找到了适用于我的实例的解决方案。之所以有效,是因为我正在使用以下代码创建viewControllers:
if let stbrd = self.storyboard {
if let vc: ViewController = stbrd.instantiateViewController(withIdentifier: "VC") as? ViewController {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil) // remove back button text
self.navigationItem.backBarButtonItem?.tintColor = DisplayUtils.sweetGreenColor()
self.navigationController?.pushViewController(vc, animated: true)
}
}
是这行:
self.navigationItem.backBarButtonItem?.tintColor = DisplayUtils.sweetGreenColor()
正确设置了新显示的视图控制器上向后箭头的颜色。
我仍然不知道如何像在此更改之前那样在视图控制器本身上简单地设置该颜色。因此,如果任何人都可以提供该知识,那就太好了。