1

I can adjust all the other aspects of the appearance of my navigation bar - but the font of 'Back' remains stubborn.

The MWE below shows four things I have tried to no avail

1)

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 4)!], for: .normal)
    return true
}

2) 3) 4)

class customNavigationController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()


    UIBarButtonItem.appearance().setTitleTextAttributes(
    [
        NSAttributedString.Key.font : UIFont(name: "Rockwell", size: 4)!,
        NSAttributedString.Key.foregroundColor : UIColor.white,
    ], for: .normal )

    navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 7)!], for: .normal)

    navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 4)!], for: .normal)
}

}

2 Answers2

3

It's simplest in iOS 13:

let app = UINavigationBarAppearance()
app.backButtonAppearance.normal.titleTextAttributes = [
    // whatever
]
UINavigationBar.appearance().standardAppearance = app

Before iOS 13, the API doesn't draw the distinction you want to draw. You just have to set the individual bar button item title text attributes for all your back buttons one at a time.

let title = // ...
let back = UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
back.setTitleTextAttributes([
    // whatever
], for: .normal)
self.navigationItem.backBarButtonItem = back

(Remember also that your back bar button item is not the back bar button item when this view controller is visible, but when another view controller is pushed on top of this one.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

This solution seems to work well for me:

// Set all fonts in the navigation controller
class CustomNavigationController: UINavigationController {
    // Font names
    let normalFontName = "AppleSDGothicNeo-Medium"
    let boldFontName = "AppleSDGothicNeo-Bold"

    // Font size
    let fontSize = CGFloat(13)

    // Create fonts
    let backButtonFont = UIFont(name: normalFontName, size: fontSize)
    let titleFont = UIFont(name: boldFontName, size: fontSize)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set standard appearance
        let appearance = UINavigationBarAppearance()
        appearance.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: backButtonFont]
        appearance.titleTextAttributes = [NSAttributedString.Key.font: titleFont]
        UINavigationBar.appearance().standardAppearance = appearance
    }
}
Amy
  • 1,318
  • 3
  • 12
  • 24
  • 1
    I think this would work - but UINavigationBarAppearance is only available ios13 onwards... still stuck for a retro fit... – Mike Albertson Nov 13 '19 at 16:35