如何更改未选中的UITabBarItem标题和背景图像iOS 8的tintColor?
未选择状态的默认颜色为浅灰色,但在我的暗色UITabBar背景上不显示
我希望我未选择状态的颜色为[UIColor blackColor]
在我的app委托中,didfinishlaunchingwithoptions:我有
UIImage *deselectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *selectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
e.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Profile" image:deselectedE selectedImage:selectedE];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
答案 0 :(得分:46)
想出来了!
使用它来更改文字的颜色:
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateSelected];
确保图像的图像渲染模式设置为ORIGINAL
UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
答案 1 :(得分:22)
在application didFinishLaunchingWithOptions:
内的AppDelegate.m中,使用以下代码:
//unselected icon tint color
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
//selected tint color
[[UITabBar appearance] setTintColor:[UIColor greenColor]];
//text tint color
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateNormal];
//background tint color
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];
答案 2 :(得分:14)
答案 3 :(得分:0)
您也可以直接在故事板中进行设置...在此处查看我的答案: How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)
如果您正在使用Storyboard,则还可以为
Bar Item
设置Selected Image
,为Selected Bar Item
设置Assets
,以便在tabBar中获得未更改的图片。
或者在Render As
目录中,您可以在图片的属性中选择Original Image
:View
(Utilities
> Show Attributes Inspector
> {{1 }或快捷方式⌥⌘4
(Option + Command + 4))