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.
我有一个使用故事板的项目,每当我用segue推视图控制器时,动态创建的bar按钮项总是蓝色的。
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中设置它的颜色(就像我对以前的bar按钮项所做的那样)。
Among the solutions I have tried are:
我尝试过的解决方案包括:
- Set it in the receiver's
viewDidLoad
- 将它设置在接收者的viewDidLoad中
-
Set it in the receiver's
viewDidAppear
将它设置在接收者的viewDidAppear中
self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
self.navigationItem.backBarButtonItem。tintColor =[用户界面颜色whiteColor];
-
When I saw that didn't quite work, I tried setting the leftBarButtonItem instead:
当我看到这并不奏效时,我试着设置leftBarButtonItem:
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem。tintColor =[用户界面颜色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]];
[[UIBarButtonItem外观]setTintColor:(用户界面颜色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是否有变化?无论我如何将“分类”设置为白色,它总是默认的蓝色。
6 个解决方案
#1
80
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.
在ios7中,要设置应用程序中所有barButtonItems的颜色,请在AppDelegate中的应用程序窗口中设置tintColor属性。
- (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).
更详细的信息在苹果的ios7用户界面过渡指南(特别是在“使用色彩”部分)。
***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;
}
#2
21
I think you are looking for a property of your UINavigationBar. Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
我想你在找你的UINavigationBar。尝试设置self.navigationController.navigationBar。tintColor =[用户界面颜色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。
#3
9
In Swift 3.0
在斯威夫特3.0
let navigationBarAppearnce = UINavigationBar.appearance()
A navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images.
导航条的色调会影响后指示符图像、按钮标题和按钮图像的颜色。
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属性影响bar本身的颜色
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
}
#4
1
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
在ios8中,如果你为了某种目的改变了UIView tint颜色,例如为UIAlertView品牌,UIToolBar中的UIBarButtonItem的tint颜色也改变了。要解决这个问题,只需编写这个代码。
[UIView appearance].tintColor = SOME_COLOR;
[UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;
For UIBarButtonItem tint color in UINavigationBar use standard method
对于UINavigationBar中的UIBarButtonItem tint颜色使用标准方法
[UINavigationBar appearance].tintColor = BLACK_COLOR;
#5
0
UITabBar.appearance().tintColor = UIColor.yellowColor()
#6
0
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];
#1
80
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.
在ios7中,要设置应用程序中所有barButtonItems的颜色,请在AppDelegate中的应用程序窗口中设置tintColor属性。
- (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).
更详细的信息在苹果的ios7用户界面过渡指南(特别是在“使用色彩”部分)。
***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;
}
#2
21
I think you are looking for a property of your UINavigationBar. Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
我想你在找你的UINavigationBar。尝试设置self.navigationController.navigationBar。tintColor =[用户界面颜色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。
#3
9
In Swift 3.0
在斯威夫特3.0
let navigationBarAppearnce = UINavigationBar.appearance()
A navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images.
导航条的色调会影响后指示符图像、按钮标题和按钮图像的颜色。
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属性影响bar本身的颜色
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
}
#4
1
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
在ios8中,如果你为了某种目的改变了UIView tint颜色,例如为UIAlertView品牌,UIToolBar中的UIBarButtonItem的tint颜色也改变了。要解决这个问题,只需编写这个代码。
[UIView appearance].tintColor = SOME_COLOR;
[UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;
For UIBarButtonItem tint color in UINavigationBar use standard method
对于UINavigationBar中的UIBarButtonItem tint颜色使用标准方法
[UINavigationBar appearance].tintColor = BLACK_COLOR;
#5
0
UITabBar.appearance().tintColor = UIColor.yellowColor()
#6
0
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];