我的应用程序中有一个浅色背景的UIBarButtonItems,我想用较暗的颜色而不是标准白色着色图标。
像这样:
我能够使用
对文本进行着色[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor blackColor]} forState:UIControlStateNormal];,
但我无法弄清楚如何更改图标颜色。
有可能吗?
答案 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];