如何更改导航栏后退按钮的字体和颜色

时间:2021-07-27 07:38:51

标签: swift swiftui swiftui-navigationview

我正在尝试更改导航栏按钮的字体和颜色。

Screenshot

理想情况下,我想将其更改为黑色并使用我已经设置的自定义字体。有没有办法修改现有的按钮?

我在字体的视图初始化中尝试了这个

DEBUG:root:results['data']['geometry']: {"type":"Polygon","coordinates":[[[6.66079022243348,51.140794993202],[6.66079005460558,51.1407983122385],[6.66081135709435,51.1407987385067],[6.66080873391236,51.1407981018504],[6.66079022243348,51.140794993202]]]}
DEBUG:root:geojson: {"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": "{\"type\":\"Polygon\",\"coordinates\":[[[6.66079022243348,51.140794993202],[6.66079005460558,51.1407983122385],[6.66081135709435,51.1407987385067],[6.66080873391236,51.1407981018504],[6.66079022243348,51.140794993202]]]}", "properties": {"AreaOfCoverage": "0.647782903636197", "ETRa_soil": "0.0400227641046727", "ETRc_soil": "7.65935710118916", "PECgw": "4168.4374316502"}}]}

但我认为这只会改变内联导航栏的标题

2 个答案:

答案 0 :(得分:3)

这是一个 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
}

答案 1 :(得分:1)

你可以按照上面的方法或者:

转到您的资产 -> 为 Accent 设置颜色,尽管这会影响您应用中的其他组件,例如 DisclosureGroup 按钮等。