简体   繁体   中英

iOS 13 UIBarButtonItem Font

Wanting to have my own custom font on navigation bar back button items, This worked for me in the past:

    let barButtonAttributes = [NSAttributedString.Key.foregroundColor : UIColor.pink,
                               NSAttributedString.Key.font : UIFont(name: "My-Awesome-Font", size: 18)!]
    UIBarButtonItem.appearance().setTitleTextAttributes(barButtonAttributes, for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes(barButtonAttributes, for: .highlighted)

With iOS 13, this has stopped working for me. Is there a work around?

You'll want to look into the new UINavigationBarAppearance classes in iOS 13.

In your case:

let style = UINavigationBarAppearance()
style.buttonAppearance.normal.titleTextAttributes = [.font: #YOURFONT#]
style.doneButtonAppearance.normal.titleTextAttributes = [.font: #YOURFONT#]

navigationController?.navigationBar.standardAppearance = style
// You may want these as well:
navigationController?.navigationBar.scrollEdgeAppearance = ...
navigationController?.navigationBar.compactAppearance = ...

Edit: Documentation - https://developer.apple.com/documentation/uikit/uinavigationbarappearance

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