选择或突显了在全球范围内改变的UIBarButtonItem文本颜色

问题描述 投票:1回答:3

我有UIToolbar(我UINavigationController内)与它的一些UIBarButtonItems。

工具栏是blueisTranslucent = false),按钮white。默认高亮颜色是gray,它看起来丑陋。

我发现的唯一的解决办法是改变UIBarButtonItemtintColor@IBAction触摸事件。但是我有很多UIBarButtonItems其烦写了大量的代码在每一个@IBAction

你知道如何在全球范围内改变它,或者至少,子类UIBarButtonItem一次定义它到处使用这个类?

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

ios swift uibarbuttonitem
3个回答
4
投票

您的didFinishLaunchingWithOptions巫婆你AppDelegate函数告诉代表,此次推出的过程几乎完成,应用程序几乎可以运行。

而在那里,你可以使用appearanceUIBarButtonItemUINavigationBar

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
}

结果: enter image description here


3
投票

您可以使用appearance在全球范围内改变颜色。添加在didFinishLaunchingWithOptions方法下一行

UIBarButtonItem.appearance().setTitleTextAttributes(
    [NSForegroundColorAttributeName: UIColor.white], for: .selected)
UIBarButtonItem.appearance().setTitleTextAttributes(
    [NSForegroundColorAttributeName: UIColor.white], for: .highlighted)

0
投票

雨燕4.2

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.darkGray], for: .selected)
© www.soinside.com 2019 - 2024. All rights reserved.