选择或突出显示时全局更改 UIBarButtonItem 文本颜色

Joh*_*kon 1 uibarbuttonitem ios swift

UIToolbar(在我的里面UINavigationController)有一些UIBarButtonItems 。

工具栏为blue( isTranslucent = false),按钮为white。默认的高亮颜色是gray,它看起来很难看。

我发现的唯一解决方案是更改触摸UIBarButtonItem事件。但我有很多s,在每个 s 中编写大量代码非常烦人。tintColor@IBActionUIBarButtonItem@IBAction

您是否知道如何全局更改它,或者至少知道如何子类化UIBarButtonItem以定义一次并在任何地方使用该类?

我使用 swift 3,部署目标是 iOS 9+

Ras*_*n L 5

您的女巫didFinishLaunchingWithOptions中的函数AppDelegate告诉代表启动过程即将完成并且应用程序即将准备好运行。

在那里你可以appearance在你的UIBarButtonItem和上使用UINavigationBar

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Text
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .selected)

    // Images
    UINavigationBar.appearance().tintColor = UIColor.orange

    return true
}
Run Code Online (Sandbox Code Playgroud)

结果:
在此输入图像描述