iOS 13 UIBarButtonItem字体

时间:2019-09-16 19:05:34

标签: ios swift uinavigationcontroller uinavigationbar uibarbuttonitem

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

    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上,这对我来说已停止工作。有解决方法吗?

1 个答案:

答案 0 :(得分: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