![](/img/trans.png)
[英]Change the font on the back button of a navigation controller under swift 4
[英]How to change font size of Navigation controller back button
我试图将导航控制器后退按钮的字体大小更改为无效。
我目前正在以前的视图控制器viewWillAppear
执行此操作:
navigationController?.navigationBar.backItem?.backBarButtonItem?.setTitleTextAttributes(
[NSAttributedStringKey.font : UIFont.systemFont(ofSize: 5)], for: .normal)
上面的代码什么都不做。。所以,有两个问题。
首先,什么是更改后退按钮字体大小的正确方法(顺便说一句,这似乎比应做的要困难得多)。
其次,是否可以在单个位置(如AppDelegate)进行此更改,以使其影响所有视图控制器?
您可以在AppDelegate中更改它:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "SF Pro Display", size: 17)!], for: .normal)
return true
}
您可以在其中自定义导航栏按钮的字体和大小,以便在所有View Controller中对其进行更改。
我将使用UIAppearance协议来实现这一点。
首先,什么是更改后退按钮字体大小的正确方法(顺便说一句,这似乎比应做的要困难得多)。
这取决于用例。 如果只有一个导航栏,则可以通过执行以下操作来更改其后退按钮属性。
navigationItem.leftBarButtonItem?.setTitleTextAttributes
但是如果我想自己做一个自定义按钮,那么我只是在这里添加一个新按钮。
其次,是否可以在单个位置(如AppDelegate)进行此更改,以使其影响所有视图控制器?
如果您要更改整个条形按钮的外观,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)
我不会这样做。 从长远来看,它将无法扩展。 您最终肯定会拥有更多的导航屏幕,并且您不希望它们具有相同的外观,然后这将是一个更大的重构!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.