【问题标题】:How can I change the font and color of the Navigation Bar back button如何更改导航栏后退按钮的字体和颜色
【发布时间】:2021-07-27 07:38:51
【问题描述】:
我正在尝试更改导航栏按钮的字体和颜色。
理想情况下,我想将其更改为黑色并使用我已经设置的自定义字体。有没有办法修改现有的按钮?
我在我的视图的字体初始化中尝试了这个
UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
但我认为这只会改变内联导航栏的标题
【问题讨论】:
标签:
swift
swiftui
swiftui-navigationview
【解决方案1】:
这是一个UIBarButtonItem,所以设置这个属性。
init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}
或者你也可以使用它,
init() {
let standard = UINavigationBarAppearance()
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}
【解决方案2】:
你可以做上面的方法或者:
转到您的资产 -> 为 Accent 设置颜色,尽管这会影响您应用中的其他组件,即 DisclosureGroup 按钮等。