我有UIToolbar
(我UINavigationController
内)与它的一些UIBarButtonItem
s。
工具栏是blue
(isTranslucent = false
),按钮white
。默认高亮颜色是gray
,它看起来丑陋。
我发现的唯一的解决办法是改变UIBarButtonItem
的tintColor
在@IBAction
触摸事件。但是我有很多UIBarButtonItem
s其烦写了大量的代码在每一个@IBAction
。
你知道如何在全球范围内改变它,或者至少,子类UIBarButtonItem
一次定义它到处使用这个类?
我使用SWIFT 3,部署目标是iOS的9+
您的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
}
您可以使用appearance在全球范围内改变颜色。添加在didFinishLaunchingWithOptions
方法下一行
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .selected)
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.darkGray], for: .selected)