iOS 6,如何着色UIBarButtonItem白色图标?

时间:2013-10-17 13:29:39

标签: iphone ios objective-c uibarbuttonitem

我的应用程序中有一个浅色背景的UIBarButtonItems,我想用较暗的颜色而不是标准白色着色图标。

像这样:

enter image description here

我能够使用

对文本进行着色
[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor blackColor]} forState:UIControlStateNormal];,

但我无法弄清楚如何更改图标颜色。

有可能吗?

3 个答案:

答案 0 :(得分:3)

使用自定义图片创建UIButton,然后使用UIBarButtonItem创建UIButton作为自定义视图:

UIImage *buttonImage = [UIImage imageNamed:@"image"]; // Tint this image programmatically
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

请注意,这主要适用于iOS 6及更低版本。在iOS 7+中,您可以使用tintColor免费获得此行为。

答案 1 :(得分:1)

我相信这可能在导航栏中有效:

[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

[[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]];

当然,您也可以在故事板选项中更改整体色调(不在我的Mac上,因此无法确切地告诉您在哪里)

答案 2 :(得分:0)

使用以下代码可能对您的情况有所帮助。

[myBarButton_name setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor yellowColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];