首页 > 代码库 > IOS7以后无需自定义,改变UITabbarItem的图片文字颜色

IOS7以后无需自定义,改变UITabbarItem的图片文字颜色

在IOS7以前,UITabbarItem的图片都是被固定渲染为蓝色,想要改变UITabbarItem的图片颜色就必须要自定义,在IOS7以后,得到了更新,方便大家自己去设定颜色,下面给出代码!

1、创建UITabbarItem的默认图片和选中图片

//第一个界面        ChildViewController *childvc = [[ChildViewController alloc]initWithNibName:nil bundle:nil];    UINavigationController *childnav = [[UINavigationController alloc]initWithRootViewController:childvc];   // UITabBarItem *childItem = [[UITabBarItem alloc]init];    UITabBarItem *childItem = [[UITabBarItem alloc]initWithTitle:@"孩子情况" image:[UIImage imageNamed:@"关于孩子 默认效果.png"] tag:1];    childItem.selectedImage = [[UIImage imageNamed:@"关于孩子 选中效果.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    childnav.tabBarItem = childItem;

初始化的方法最好是用这个

- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag;

其中

    childItem.selectedImage = [[UIImage imageNamed:@"关于孩子 选中效果.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

这句话就是设置选中时候的图片,始终让图片保持原样不被渲染为蓝色。

imageWithRenderingMode是UIImage的一个渲染模式,着色(Tint Color)是iOS7界面中的一个.设置UIImage的渲染模式:UIImage.renderingMode重大改变,你可以设置一个UIImage在渲染时是否使用当前视图的Tint Color。UIImage新增了一个只读属性:renderingMode,对应的还有一个新增方法:imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属性。该枚举中包含下列值:
  1. UIImageRenderingModeAutomatic  // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。  
  2. UIImageRenderingModeAlwaysOriginal   // 始终绘制图片原始状态,不使用Tint Color。  
  3. UIImageRenderingModeAlwaysTemplate   // 始终根据Tint Color绘制图片,忽略图片的颜色信息。  
 

renderingMode属性的默认值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取决于它显示的位置。

 

2、设置UITabbarItem的title的颜色不被渲染

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];    [[UITabBarItem appearance] setTitleTextAttributes:                                                         [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:59.0/255.0 green:207.0/255.0 blue:202.0/255.0 alpha:1],UITextAttributeTextColor, nil]forState:UIControlStateSelected];    

其中上面的那句是设置默认颜色的,下面的时设置选中后的字体的颜色。

 

 

下面是完整的AppDelegate.m方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    //第一个界面        ChildViewController *childvc = [[ChildViewController alloc]initWithNibName:nil bundle:nil];    UINavigationController *childnav = [[UINavigationController alloc]initWithRootViewController:childvc];   // UITabBarItem *childItem = [[UITabBarItem alloc]init];    UITabBarItem *childItem = [[UITabBarItem alloc]initWithTitle:@"孩子情况" image:[UIImage imageNamed:@"关于孩子 默认效果.png"] tag:1];    childItem.selectedImage = [[UIImage imageNamed:@"关于孩子 选中效果.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    childnav.tabBarItem = childItem;    //第二个界面    KKViewController *chatvc = [[KKViewController alloc]initWithNibName:nil bundle:nil];    UINavigationController *chatNav = [[UINavigationController alloc]initWithRootViewController:chatvc];    UITabBarItem *chatItem = [[UITabBarItem alloc]initWithTitle:@"聊天" image:[UIImage imageNamed:@"沟通 默认.png"] tag:2];        chatItem.selectedImage = [[UIImage imageNamed:@"沟通 选中.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    chatNav.tabBarItem = chatItem;        //第三个界面    ToolsViewController *toolvc = [[ToolsViewController alloc]initWithNibName:nil bundle:nil];    UINavigationController *toolNav = [[UINavigationController alloc]initWithRootViewController:toolvc];    UITabBarItem *toolItem = [[UITabBarItem alloc]initWithTitle:@"工具栏" image:[UIImage imageNamed:@"工具栏 默认.png"] tag:3];    toolItem.selectedImage = [[UIImage imageNamed:@"工具栏 选中.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    toolNav.tabBarItem = toolItem;    //第四个界面    PersonViewController *loginvc = [[PersonViewController alloc]initWithNibName:nil bundle:nil];    UINavigationController *loginnav = [[UINavigationController alloc]initWithRootViewController:loginvc];    UITabBarItem *loginItem = [[UITabBarItem alloc]initWithTitle:@"个人中心" image:[UIImage imageNamed:@"个人中心 默认.png"] tag:4];    loginItem.selectedImage = [[UIImage imageNamed:@"个人中心 选中.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    loginnav.tabBarItem = loginItem;        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];    [[UITabBarItem appearance] setTitleTextAttributes:                                                         [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:59.0/255.0 green:207.0/255.0 blue:202.0/255.0 alpha:1],UITextAttributeTextColor, nil]forState:UIControlStateSelected];            UITabBar *tb=[[UITabBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 96.0) ] ;    [self.window addSubview:tb];    UITabBarController *tbc = [[UITabBarController alloc]init];    tbc.viewControllers = @[childnav,chatNav,toolNav,loginnav];         self.window.rootViewController = tbc;    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

 

有疑问的请留言相互交流!

IOS7以后无需自定义,改变UITabbarItem的图片文字颜色