简体   繁体   中英

Change the font on the back button of a navigation controller under swift 4

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]"

在此处输入图片说明

This is in viewDidLoad method. Should this work?

For Swift 4, U can 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

在此处输入图片说明

Output

在此处输入图片说明

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)
}

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)

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).

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM