ios 更改 UIBarButtonItem 的色调颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19504291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the Tint Color of UIBarButtonItem
提问by Andy Ibanez
I have a project using Storyboards and whenever I push a view controller with a segue, the dynamically created bar button item is always blue.
我有一个使用 Storyboards 的项目,每当我使用 segue 推送视图控制器时,动态创建的栏按钮项始终为蓝色。
It's driving me nuts. Because this object is created dynamically, I cannot set its color in IB (like I have done with previous bar button items).
它让我发疯。因为这个对象是动态创建的,所以我不能在 IB 中设置它的颜色(就像我对以前的栏按钮项目所做的那样)。
Among the solutions I have tried are:
我尝试过的解决方案包括:
- Set it in the receiver's
viewDidLoad
Set it in the receiver's
viewDidAppear
self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
When I saw that didn't quite work, I tried setting the leftBarButtonItem instead:
- 将其设置在接收器的
viewDidLoad
将其设置在接收器的
viewDidAppear
self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
当我看到这不太奏效时,我尝试改为设置 leftBarButtonItem:
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
I have tried the following code (which I got from other SO answers) in my app's delegate, when the new view gets called, and before pushing the new view:
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
当新视图被调用时,以及在推送新视图之前,我在我的应用程序委托中尝试了以下代码(我从其他 SO 答案中获得):
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
All the google answers I have found recommend to use the code above, but it's not working at all for me. Maybe there are some changes in iOS 7's appearance API? No matter how or where I try to set "Categorías" to white, it's always the default blue.
我发现的所有谷歌答案都建议使用上面的代码,但它对我来说根本不起作用。也许 iOS 7 的外观 API 有一些变化?无论我如何或在何处尝试将“Categorías”设置为白色,它始终是默认的蓝色。
回答by hgwhittle
In iOS 7, to set the color of all barButtonItems in your app, set the tintColor
property on the application's window in the AppDelegate.
在 iOS 7 中,要设置应用程序中所有 barButtonItems 的颜色,请tintColor
在 AppDelegate 中设置应用程序窗口上的属性。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.tintColor = [UIColor whiteColor];
return YES;
}
More detailed info in Apple's iOS 7 UI Transition Guide(Specifically under the 'Using Tint Color` section).
Apple 的 iOS 7 UI 过渡指南中的更多详细信息(特别是在“使用色调颜色”部分)。
***OR***
***或者***
Based on some of the comments, you can also achieve this with the UINavigationBar appearance proxy. This will affect the tintColor of only UIBarButtonItems, as opposed to setting the tintColor on the window and affecting all subviews of that window.
根据一些评论,您还可以使用 UINavigationBar 外观代理来实现这一点。这将仅影响 UIBarButtonItems 的 tintColor,而不是在窗口上设置 tintColor 并影响该窗口的所有子视图。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
}
return YES;
}
回答by Yuriy Panfyorov
I think you are looking for a property of your UINavigationBar. Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
我认为您正在寻找 UINavigationBar 的属性。尝试设置self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
See "Appearance of Navigation Bars" section: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1
请参阅“导航栏的外观”部分:https: //developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1
回答by Ashok R
In Swift 3.0
在 Swift 3.0 中
let navigationBarAppearnce = UINavigationBar.appearance()
A navigation bar's tintColoraffects the color of the back indicator image, button titles, and button images.
导航栏的tintColor会影响背面指示器图像、按钮标题和按钮图像的颜色。
navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
The barTintColor property affects the color of the bar itself
barTintColor 属性影响条形本身的颜色
navigationBarAppearnce.tintColor = UIColor.white
Final Code
最终代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navigationBarAppearnce = UINavigationBar.appearance()
navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
navigationBarAppearnce.tintColor = UIColor.white
navigationBarAppearnce.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
//Change status bar color
UIApplication.shared.statusBarStyle = .lightContent
return true
}
回答by Mithra Singam
Swift 5
斯威夫特 5
barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
回答by FunkyKat
In iOS 8 if you changed UIView tint color for some purpose, for example for branding UIAlertView, tint color for UIBarButtonItem in UIToolBar also changed that way. To fix this, just write this code
在 iOS 8 中,如果您出于某种目的更改了 UIView 色调颜色,例如为 UIAlertView 品牌化,UIToolBar 中 UIBarButtonItem 的色调颜色也会以这种方式更改。要解决此问题,只需编写此代码
[UIView appearance].tintColor = SOME_COLOR;
[UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;
For UIBarButtonItem tint color in UINavigationBar use standard method
对于 UINavigationBar 中的 UIBarButtonItem 色调颜色使用标准方法
[UINavigationBar appearance].tintColor = BLACK_COLOR;
回答by Vette
To change the color of a specific item (e.g. a button) in your nav bar: In Objective-C
要更改导航栏中特定项目(例如按钮)的颜色:在 Objective-C 中
myButton.tintColor = [UIColor redColor];
回答by yehyatt
This worked for me
这对我有用
AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
回答by user2643679
UITabBar.appearance().tintColor = UIColor.yellowColor()