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

5

我的应用程序中有带浅色背景的UIBarButtonItems,并且我想将图标着色为较暗的颜色,而不是标准的白色。

就像这样:

enter image description here

我能够使用

[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor blackColor]} forState:UIControlStateNormal];,

但是我不知道如何更改图标的颜色。

这个有可能吗?


可能是重复的问题:我可以使用带有彩色图像的UIBarButtonItem吗? - Toseef Khilji
+1。从来没有注意到这件事。 - Bhavin
3个回答

3
创建一个带有自定义图片的 `UIButton`,然后使用该 `UIButton` 作为自定义视图创建一个 `UIBarButtonItem`:
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
我相信这可能适用于导航栏:
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

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

当然,您也可以在故事板选项中更改整个色调(我的Mac上没有,所以无法告诉您确切位置)。

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

@Guilherme http://stackoverflow.com/questions/4518617/setting-bar-button-item-color-in-a-custom-navigation-bar 和 https://dev59.com/tmYr5IYBdhLWcg3wb5vc - iPatel
不,我想要改变按钮内部图标/图片的颜色。 就像这样:http://i.stack.imgur.com/AUPfh.png。 - Guilherme
1
最好的方法是创建自定义按钮并添加它。我尝试在谷歌上找到您的解决方案,但无法找到:( - iPatel
@iPatel,您不需要自定义按钮来给标准的UIBarButtonItem上色。只需设置按钮的tintColor即可。 - rmaddy
@rmaddy - 但是使用tintColor颜色,我们可以改变UIBarButtonItem的颜色而不是图像颜色。 OP只想改变内置UIBarButtonItem图像的颜色。就像http://i.stack.imgur.com/AUPfh.png这样。 - iPatel
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接