更改UIBarButtonItem的色调颜色 [英] Changing the Tint Color of UIBarButtonItem

查看:582
本文介绍了更改UIBarButtonItem的色调颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Storyboard的项目,每当我用segue推动一个视图控制器时,动态创建的条形按钮项始终为蓝色。

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.

这让我疯了。因为这个对象是动态创建的,所以我不能在IB中设置它的颜色(就像我以前的条形按钮项目一样)。

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).

我尝试过的解决方案包括:

Among the solutions I have tried are:


  1. 将其设置在收件人的 viewDidLoad

  2. 将其设置在接收者的 viewDidAppear

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

当我看到它不起作用时,我尝试设置leftBarButtonItem:

When I saw that didn't quite work, I tried setting the leftBarButtonItem instead:

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


  1. 我在我的应用程序中尝试了以下代码(我从其他SO答案中获得)委托,当调用新视图时,以及在推送新视图之前:

  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:

[[UIBarButtonItem外观] setTintColor:[UIColor whiteColor]];

所有goog le答案我发现建议使用上面的代码,但它对我来说根本不起作用。也许iOS 7的外观API有一些变化?无论我如何或在何处尝试将Categorías设置为白色,它始终是默认的蓝色。

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中,要设置应用中所有barButtonItem的颜色,请在AppDelegate的应用程序窗口中设置 tintColor 属性。

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.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.tintColor = [UIColor whiteColor];
    return YES;
}

Apple的iOS 7 UI过渡指南(特别是在'使用Tint Color`部分)。

More detailed info in Apple's iOS 7 UI Transition Guide (Specifically under the 'Using Tint Color` section).

***或***

根据一些注释,您还可以使用UINavigationBar外观代理实现此目的。这将影响仅UIBarButtonItems的tintColor,而不是在窗口上设置tintColor并影响该窗口的所有子视图。

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.

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

    return YES;
}

这篇关于更改UIBarButtonItem的色调颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆