5

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

像这样:

在此处输入图像描述

我能够使用

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

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

有可能这样做吗?

4

3 回答 3

3

UIButton使用您的自定义图像创建一个,然后使用UIBarButtonItemUIButton定义视图创建一个:

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.

于 2014-07-16T19:24:13.910 回答
1

我相信这可能在导航栏中起作用:

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

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

当然,您还可以更改情节提要选项中的整个色调(不是在我的 Mac 上,所以无法告诉您确切的位置)

于 2013-10-17T14:45:48.537 回答
0
[myBarButton_name setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor yellowColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
于 2013-10-17T13:33:16.083 回答