简体   繁体   English

更改 UIBarButtonItem 的色调颜色

[英]Changing the Tint Color of UIBarButtonItem

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:我尝试过的解决方案包括:

  1. Set it in the receiver's viewDidLoad在接收者的viewDidLoad
  2. Set it in the receiver's viewDidAppear在接收者的viewDidAppear

    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];

  3. When I saw that didn't quite work, I tried setting the leftBarButtonItem instead:当我看到这不太奏效时,我尝试改为设置 leftBarButtonItem:

self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

  1. 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:当新视图被调用时,以及在推送新视图之前,我在我的应用程序委托中尝试了以下代码(我从其他 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?也许 iOS 7 的外观 API 有一些变化? No matter how or where I try to set "Categorías" to white, it's always the default blue.无论我如何或在何处尝试将“Categorías”设置为白色,它始终是默认的蓝色。

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 的颜色,请在 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). Apple 的 iOS 7 UI 过渡指南中的更多详细信息(特别是在“使用色调颜色”部分)。

***OR*** ***或***

Based on some of the comments, you can also achieve this with the UINavigationBar appearance proxy.根据一些评论,您还可以使用 UINavigationBar 外观代理来实现这一点。 This will affect the tintColor of only UIBarButtonItems, as opposed to setting the tintColor on the window and affecting all subviews of that window.这将仅影响 UIBarButtonItems 的 tintColor,而不是在窗口上设置 tintColor 并影响该窗口的所有子视图。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
        [UINavigationBar appearance].tintColor = [UIColor whiteColor];
    }

    return YES;
}

I think you are looking for a property of your UINavigationBar.我认为您正在寻找 UINavigationBar 的属性。 Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];尝试设置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

In Swift 3.0在 Swift 3.0 中

let navigationBarAppearnce = UINavigationBar.appearance()

A navigation bar's tintColor affects 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
}

在此处输入图片说明

斯威夫特 5

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

要更改导航栏中特定项目(例如按钮)的颜色:在 Objective-C 中

myButton.tintColor = [UIColor redColor];

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.在 iOS 8 中,如果您出于某种目的更改了 UIView 色调颜色,例如为 UIAlertView 品牌化,UIToolBar 中 UIBarButtonItem 的色调颜色也会以这种方式更改。 To fix this, just write this code要解决此问题,只需编写此代码

[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;
UITabBar.appearance().tintColor = UIColor.yellowColor()

这对我有用

AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM