swift - NavBar 上的后退按钮上的字体(Swift)
问题描述
我可以调整导航栏外观的所有其他方面 - 但“返回”的字体仍然很顽固。
下面的 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)
类 customNavigationController: 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
(还请记住,当此视图控制器可见时,您的后退栏按钮项目不是后退栏按钮项目,而是当另一个视图控制器被推到此视图控制器之上时。)
推荐阅读
- laravel - 如何在 Laravel 中启用条带生产模式?
- dart - 使用 VS Code 时,有没有办法查看我在当前 dart 项目中定义的所有类/函数/全局变量的列表?
- html - 为什么我的 html/css 导航栏没有跳转到给定的类?
- mysql - MYSQL获取总和列
- c# - c#将字节添加到bytearray?
- r - R rect.hclust:树状图中的矩形太高
- android - android.text.html (fromHtml): 渲染内联 CSS 颜色
- python - C++ boost.python 无法将 const char* 转换为 str
- python - 检查是否为二进制并转换为十进制
- python - 在python中使用win32com在工作簿中插入行不起作用