更改未select的UITabBarController项目标题和背景图像的tintColor
我怎样才能改变一个未经select的UITabBarItem标题和背景图像iOS 8的tintColor?
未select状态的默认颜色是浅灰色,但不会显示在我的深色阴影UITabBar背景上
我希望我的未选中状态具有[UIColor blackColor]的颜色
在我的应用程序代表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]];
弄清楚了!
使用这个来改变文本的颜色:
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] } forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] } forState:UIControlStateSelected];
并确保图像呈现模式设置为原始的图像
UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
在你的AppDelegate.m里面的application didFinishLaunchingWithOptions:
使用下面的代码:
//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]];
您也可以在资产文件的属性检查器中将图像呈现为原始图像,而无需编写任何代码
你也可以直接在Storyboard中设置它…检查我的答案在这里: 如何设置UITabBarItem的未选中的色调,***包括系统项***(iOS7)
如果您正在使用Storyboard,则还可以同时设置
Image
forBar Item
和Selected Image
Bar Item
Selected Image
以在tabBar中获取未改变的图像。
或者在Assets
目录中,您可以selectOriginal Image
属性中的“ Render As
: Original Image
图像”( View
> Utilities
> Show Attributes Inspector
或快捷键⌥⌘4
(Option + Command + 4))