无法更改导航栏的背景色。在下面的示例中,我尝试将颜色设置为navigationController?.navigationBar.backgroundColor和navigationController?.navigationBar.barTintColor,,例如UIColor.red,但是在视图层次结构中的导航栏颜色上方出现了白色的UIImage,如下图所示:https://imguh.com/image/CBXaj
也尝试使导航条半透明和不透明,没有效果。我可以更改按钮颜色和自定义其他元素,但不能更改条形条的背景色。感谢你的帮助。
发布于 2022-01-24 15:09:21
下面的代码应该对您有好处:
背景色
// This will change the navigation bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.green // your colour here
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance将其放置在viewDidLoad()函数中的ViewController中
标题颜色
如果还想更改导航栏标题外观,请在设置standardAppearance和scrollEdgeAppearance之前应用以下代码
// This will alter the navigation bar title appearance
let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.purple] //alter to fit your needs
appearance.titleTextAttributes = titleAttribute完整代码:
// This will change the navigation bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.green
// This will alter the navigation bar title appearance
let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.purple] //alter to fit your needs
appearance.titleTextAttributes = titleAttribute
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearancehttps://stackoverflow.com/questions/70834421
复制相似问题