如何为 AppDelegate 中的所有视图控制器独立更改左右栏按钮项文本属性?

Posted

技术标签:

【中文标题】如何为 AppDelegate 中的所有视图控制器独立更改左右栏按钮项文本属性?【英文标题】:How to change the left and right bar button items text attributes independently for all View Controllers in AppDelegate? 【发布时间】:2018-06-15 05:50:59 【问题描述】:

在我的应用程序中,我在 UINavigationController 中嵌入了许多 UIViewControllers,其中包含 取消 rightBarButtonItem保存 leftBarButtonItem

在每个类中,我都有重复的代码来自定义栏按钮项的属性:

navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: fontFuturaMedium,
                                                                                             size: fontSize17)! ], 
                                                         for: .normal)

navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: fontFuturaMedium,
                                                                                                 size: fontSize17)! ], 
                                                         for: . highlighted)

navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: fontFuturaBold,
                                                                                             size: fontSize19)! ], 
                                                         for: .normal)

navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: fontFuturaBold,
                                                                                                 size: fontSize19)! ], 
                                                         for: . highlighted)

AppDelegate中,我可以更改条形按钮项目,这会影响leftBarButtonItemrightBarButtonItem

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: fontFuturaMedium,
                                                                                        size: fontSize17)!],
                                                    for: .normal)

但是,我想独立地更改两个按钮,从而影响所有视图控制器,而无需复制代码。

我试图访问UINavigationItemleftBarButtonItem 属性,但它不存在。 UINavigationItem 也没有 appearance() 属性。

我已将重复代码放在全局函数中:

static public func setNavBar(viewController: UIViewController)

    viewController.navigationItem.leftBarButtonItem?.setTitleTextAttributes( [NSAttributedStringKey.font: UIFont(name: fontFuturaMedium,
                                                                                                  size: fontSize17)! ],
                                                              for: .normal)
    viewController.navigationItem.leftBarButtonItem?.setTitleTextAttributes( [NSAttributedStringKey.font: UIFont(name: fontFuturaMedium,
                                                                                                  size: fontSize17)! ],
                                                              for: .highlighted)
    viewController.navigationItem.rightBarButtonItem?.setTitleTextAttributes( [NSAttributedStringKey.font: UIFont(name: fontFuturaBold,
                                                                                                   size: fontSize19)! ],
                                                               for: .normal)
    viewController.navigationItem.rightBarButtonItem?.setTitleTextAttributes( [NSAttributedStringKey.font: UIFont(name: fontFuturaBold,
                                                                                                   size: fontSize19)! ],
                                                               for: .highlighted)

这显然要容易得多,并且减少了很多重复代码,因为我可以在我的类中的所有中调用那一行代码。

但是,我可以在 AppDelegate 中放入一个行代码,该代码将在整个类中复制所有,类似于UIBarButtonItem.appearance().setTitleTextAttributes,而不是调用@987654335 @ 在所有课程中?

谢谢。

【问题讨论】:

将代码放入UIViewController扩展的方法中。 在关闭最后一个大括号扩展 UIViewController func displayContentController(_ content:UIViewController, controllerName : String) // 在这里写你的代码 从视图控制器调用 -self.displayContentController(self, controllerName: "your wish") 【参考方案1】:

我会转类UIViewController,也许称它为BaseViewController;在基本视图控制器的viewDidLoad 中,我会放置所有设置内容(基本上是您在setNavBar 中所做的)。

然后,项目的每个视图控制器都会扩展基础视图控制器。

【讨论】:

以上是关于如何为 AppDelegate 中的所有视图控制器独立更改左右栏按钮项文本属性?的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UIPageViewController 中的所有 webViews 文本字体启用缩放

如何为自定义表格视图单元格中的按钮单击获取不同的视图控制器

如何为android中的所有视图使用相同的自定义字体?

如何为视图中的所有标签设置自定义字体? [复制]

如何处理视图控制器上的远程通知(而不是 AppDelegate)[关闭]

如何为所有 PickerViews 创建单个完成和取消按钮?