iOS 13 UIBarButtonItem 字体

iOS 13 UIBarButtonItem Font

想要在导航栏后退按钮项目上使用我自己的自定义字体,这在过去对我有用:

    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)

对于 iOS 13,这对我来说已经停止工作了。有解决办法吗?

您需要查看 iOS 13 中的新 UINavigationBarAppearance 类。

你的情况:

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

编辑: 文档 - https://developer.apple.com/documentation/uikit/uinavigationbarappearance