简体   繁体   English

如何更改导航控制器后退按钮的字体大小

[英]How to change font size of Navigation controller back button

I'm attempting to change the font size of a Navigation Controllers back button to no avail. 我试图将导航控制器后退按钮的字体大小更改为无效。

I'm currently doing this in the previous view controllers viewWillAppear : 我目前正在以前的视图控制器viewWillAppear执行此操作:

 navigationController?.navigationBar.backItem?.backBarButtonItem?.setTitleTextAttributes(
    [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 5)], for: .normal)

The above code does nothing.. So, two questions. 上面的代码什么都不做。。所以,有两个问题。

First, what is the correct approach for changing the back buttons font size (which by the way, seems far more difficult than it should be). 首先,什么是更改后退按钮字体大小的正确方法(顺便说一句,这似乎比应做的要困难得多)。

Second, is there a singular place (like the AppDelegate) I can make this change so that it affects all view controllers? 其次,是否可以在单个位置(如AppDelegate)进行此更改,以使其影响所有视图控制器?

You can change it inside 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
}

Where you can customize the font and size of the Navigation bar buttons so it changes in all View Controllers. 您可以在其中自定义导航栏按钮的字体和大小,以便在所有View Controller中对其进行更改。

I would use UIAppearance protocol to implement this. 我将使用UIAppearance协议来实现这一点。

First, what is the correct approach for changing the back buttons font size (which by the way, seems far more difficult than it should be). 首先,什么是更改后退按钮字体大小的正确方法(顺便说一句,这似乎比应做的要困难得多)。

This depends on the use-case basis. 这取决于用例。 If there is only one navigation bar, I would just change its back button properties by doing something like this. 如果只有一个导航栏,则可以通过执行以下操作来更改其后退按钮属性。

navigationItem.leftBarButtonItem?.setTitleTextAttributes

but if I want to do a custom button itself, then I would just add a new button here. 但是如果我想自己做一个自定义按钮,那么我只是在这里添加一个新按钮。

Second, is there a singular place (like the AppDelegate) I can make this change so that it affects all view controllers? 其次,是否可以在单个位置(如AppDelegate)进行此更改,以使其影响所有视图控制器?

if you want to change the entire bar button appearance, 如果您要更改整个条形按钮的外观,

UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)

I would not do this. 我不会这样做。 It will not be scalable in the longer run. 从长远来看,它将无法扩展。 You will definitely have more navigation screens eventually and you would not want them to have the same appearance and then, it would be a bigger refactor! 您最终肯定会拥有更多的导航屏幕,并且您不希望它们具有相同的外观,然后这将是一个更大的重构!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM