swift - 导航栏上后退按钮上的字体(Swift)

标签 swift uinavigationcontroller uinavigationbar uinavigationitem uinavigationbarappearance

我可以调整导航栏外观的所有其他方面 - 但“后退”的字体仍然很顽固。

下面的 MWE 显示了我尝试过但无济于事的四件事

1)

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 4)!], for: .normal)
    return true
}

2) 3) 4)

自定义导航 Controller 类:UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()


    UIBarButtonItem.appearance().setTitleTextAttributes(
    [
        NSAttributedString.Key.font : UIFont(name: "Rockwell", size: 4)!,
        NSAttributedString.Key.foregroundColor : UIColor.white,
    ], for: .normal )

    navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 7)!], for: .normal)

    navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 4)!], for: .normal)
}

最佳答案

在 iOS 13 中最简单:

let app = UINavigationBarAppearance()
app.backButtonAppearance.normal.titleTextAttributes = [
    // whatever
]
UINavigationBar.appearance().standardAppearance = app

在 iOS 13 之前,API 不会绘制您想要绘制的区分。您只需一次为所有后退按钮设置一个单独的栏按钮项目标题文本属性。

let title = // ...
let back = UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
back.setTitleTextAttributes([
    // whatever
], for: .normal)
self.navigationItem.backBarButtonItem = back

(还请记住,当这个 View Controller 可见时,您的后退栏按钮项目不是后退栏按钮项目,而是当另一个 View Controller 被推上这个的顶部。)

关于swift - 导航栏上后退按钮上的字体(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58270017/

相关文章:

ios - Swift 从 UITableViewCell 中删除存储在 UITableViewController 中的数据

ios - UITableViewCell 不会在 swift 中保持突出显示

ios - 标签栏 Controller : Orienting views in different orientations

ios - UINavigationController 隐藏导航栏

ios - 如何设置具有渐变颜色的 UINavigationbar?

ios - 如何快速添加图像作为大型导航栏上的标题?

ios - 使用 Firebase 在 iOS 13.2 中登录导航/Segue

ios - 对象数组的索引超出范围

iphone - 当呈现为模态视图 Controller 时,ARC UINavigationController 堆栈不会被释放

ios - 推送 segue 后从导航堆栈中删除 View Controller (使用 Storyboard segues)