6

Swift 4, iOS 11.2.5 Xcode 9.2

Trying to change the font of the back button. Tried previous solutions found, but none seem to work under Swift 4, iOS 11.2.5 with my configuration, navigation controller within a tab bar controller.

Got this code, the first and last lines work, but the center three do not.

self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)
navigationItem.title = "Filter \(titleSelection!) [shake to clear]"

enter image description here

This is in viewDidLoad method. Should this work?

user3069232
  • 8,587
  • 7
  • 46
  • 87

5 Answers5

14

For Swift 4, try this in AppDelegate.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 15)!], for: UIControlState.normal)
        
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .highlighted)
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .focused)
        
        return true
}

In ViewController.

override func viewWillAppear(_ animated: Bool) {
    
    self.title = "List"
    self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]

    // THE BELOW THREE LINES NOT WORKING.
 
    //navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)

    //navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)

    //navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)

}

Storyboard

enter image description here

Output

enter image description here

General Grievance
  • 4,555
  • 31
  • 31
  • 45
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
4

You could change the font in the app delegate by doing something along these lines, this will change the font through out the whole app instead of in one viewcontroller though.

if let customFont = UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20.0) {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)
}
valosip
  • 3,167
  • 1
  • 14
  • 26
4

To make it working on iOS 13.0 you should use UINavigationBarAppearience. Here is what you can do in order to change font of ALL elements in the navigation bar:

if #available(iOS 13.0, *) {
    let navBarAppearance = UINavigationBarAppearance()
    let attributes = [NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
    navBarAppearance.titleTextAttributes = attributes
    navBarAppearance.buttonAppearance.normal.titleTextAttributes = attributes
    navBarAppearance.doneButtonAppearance.normal.titleTextAttributes = attributes

    self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
    self.navigationController?.navigationBar.standardAppearance = navBarAppearance
}
Grzegorz Świerad
  • 365
  • 1
  • 2
  • 10
0

Instead of:

navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)

Try:

self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
Eric Armstrong
  • 646
  • 6
  • 17
0

In Swift 5 you can do it by these:

    let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
    self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)

Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!

Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).

Saeed Ir
  • 1,974
  • 2
  • 20
  • 22