I have UIToolbar
(inside my UINavigationController
) with some UIBarButtonItem
s on it.
我有UIToolbar(在我的UINavigationController中),上面有一些UIBarButtonItems。
Toolbar is blue
(isTranslucent = false
), button is white
. Default highlight color is gray
and it looks ugly.
工具栏为蓝色(isTranslucent = false),按钮为白色。默认高亮颜色为灰色,看起来很难看。
The only solution I found is to change UIBarButtonItem
's tintColor
in @IBAction
touch event. But I have many UIBarButtonItem
s and its pretty annoying to write a plenty of code in every @IBAction
.
我找到的唯一解决方案是在@IBAction触摸事件中更改UIBarButtonItem的tintColor。但我有很多UIBarButtonItems,并且在每个@IBAction中编写大量代码都非常烦人。
Do you know how to change it globally or, at least, subclass UIBarButtonItem
to define it once and use this class everywhere?
您是否知道如何全局更改它,或者至少是UIBarButtonItem的子类来定义它并在任何地方使用此类?
I use swift 3, deployment target is iOS 9+
我使用的是swift 3,部署目标是iOS 9+
2 个解决方案
#1
4
You have your didFinishLaunchingWithOptions
function in your AppDelegate
witch tells the delegate that the launch process is almost done and the app is almost ready to run.
你的AppDelegate中有你的didFinishLaunchingWithOptions函数,告诉代表启动过程已经完成,应用程序几乎准备好运行了。
And in there you can use the appearance
on your UIBarButtonItem
and UINavigationBar
.
在那里,您可以使用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
}
#2
3
You can use appearance to change color globally. Add next lines in didFinishLaunchingWithOptions
method
您可以使用外观全局更改颜色。在didFinishLaunchingWithOptions方法中添加下一行
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .selected)
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .highlighted)
#1
4
You have your didFinishLaunchingWithOptions
function in your AppDelegate
witch tells the delegate that the launch process is almost done and the app is almost ready to run.
你的AppDelegate中有你的didFinishLaunchingWithOptions函数,告诉代表启动过程已经完成,应用程序几乎准备好运行了。
And in there you can use the appearance
on your UIBarButtonItem
and UINavigationBar
.
在那里,您可以使用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
}
#2
3
You can use appearance to change color globally. Add next lines in didFinishLaunchingWithOptions
method
您可以使用外观全局更改颜色。在didFinishLaunchingWithOptions方法中添加下一行
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .selected)
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .highlighted)