1

I am trying to change the font and color of the Navigation Bar button.

Screenshot

Ideally, I'd like to change it to black and use a custom font that I already have set up. Is there any way to modify the existing button?

I tried this in my view's init for the font

UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]

But I think this only changes the title of an inline Nav bar

Arnav Motwani
  • 707
  • 7
  • 26

2 Answers2

4

It's a UIBarButtonItem so set this property.

init() {
    UIBarButtonItem.appearance().tintColor = .red
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}

Or you can use this also,

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
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • For second option; The created UINavigationBarAppearance instance must be assigned to the UINavigationBar.appearance().standardAppearance field. `UINavigationBar.appearance().standardAppearance = standard` – ehliaskinnesvegahi Mar 21 '23 at 11:11
1

You can do the method above or:

Go to your Assets -> Set a colour for Accent, although this will affect other components in your app i.e. DisclosureGroup button etc.

SwiftUser
  • 555
  • 6
  • 17