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

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

问题描述

我有一个使用 Storyboards 的项目,每当我使用 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. 中设置
  3. 在接收者的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 appearance] setTintColor:[UIColor whiteColor]];

我找到的所有谷歌答案都推荐使用上面的代码,但它对我来说根本不起作用.也许 iOS 7 的外观 API 有一些变化?无论我如何或在何处尝试将类别"设置为白色,它始终是默认的蓝色.

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 过渡指南(特别是在使用色调颜色"部分).

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天全站免登陆