首页 > 解决方案 > iOS 13 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 中,这对我不起作用。有解决办法吗?

标签: iosswiftuinavigationcontrolleruinavigationbaruibarbuttonitem

解决方案


您需要了解UINavigationBarAppearanceiOS 13 中的新类。

在你的情况下:

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


推荐阅读