设置 TabBarItem 选中时的图片及文字颜色

设置 TabBarItem 选中时的图片及文字颜色时主要遇到2个问题: 点击TabBarItem未能显示我所选择的图片及文字颜色

1:

原来 UIImage 在呈现(render)时会选择对应的呈现方式(render mode),ios提供了3种render mode,分别是

意义
UIImageRenderingModeAutomatic  根据图片的使用位置自动调整渲染模式(默认值)
UIImageRenderingModeAlwaysOriginal  始终绘制图片原始状态,不适用tint color。  
UIImageRenderingModeAlwaysTemplate  使用根据tint color绘制图片,忽略图片的颜色信息

 

所以处理问题如下:

 UIImage * c3SelectImge = [[UIImage imageNamed:@"tab_商家_Y"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 

2:我们可以用 text attribute 的方式设置文字样式,这样能够改变点击文字的颜色

[c3.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: clickBtnColor} forState:UIControlStateSelected];

 

 

3:上代码:

  //1.创建Window

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    

    //2.初始化一个tabBar控制器

    UITabBarController *tb=[[UITabBarController alloc]init];

    //设置控制器为Window的根控制器

    self.window.rootViewController=tb;

    

    

    //3.创建子控制器

 

   UIImage * c1SelectImge = [[UIImage imageNamed:@"tab_门锁_Y"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    DeviceViewController *c1=[[DeviceViewController alloc]init];

    c1.view.backgroundColor=[UIColor whiteColor];

    c1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设备" image:[UIImage imageNamed:@"tab_门锁_N"] selectedImage:c1SelectImge];

    [c1.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: clickBtnColor} forState:UIControlStateSelected];

    UINavigationController *n1 = [[UINavigationController alloc]initWithRootViewController:c1];

    

    UIImage * c2SelectImge = [[UIImage imageNamed:@"tab_好友_Y"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    ChatViewController *c2=[[ChatViewController alloc]init];

    c2.view.backgroundColor=[UIColor whiteColor];

    c2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"好友" image:[UIImage imageNamed:@"tab_好友_N"] selectedImage:c2SelectImge];

    [c2.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: clickBtnColor} forState:UIControlStateSelected];

    UINavigationController *n2 = [[UINavigationController alloc]initWithRootViewController:c2];

     

    UIImage * c3SelectImge = [[UIImage imageNamed:@"tab_商家_Y"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    IBeaCanViewController *c3=[[IBeaCanViewController alloc]init];

    c3.view.backgroundColor=[UIColor whiteColor];

    c3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"动态" image:[UIImage imageNamed:@"tab_商家_N"] selectedImage:c3SelectImge];

    [c3.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: clickBtnColor} forState:UIControlStateSelected];

    UINavigationController *n3 = [[UINavigationController alloc]initWithRootViewController:c3];

 

    UIImage * c4SelectImge = [[UIImage imageNamed:@"tab_设置_Y"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    MineViewController *c4=[[MineViewController alloc]init];    

    c4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"tab_设置_N"] selectedImage:c4SelectImge];

    [c4.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: clickBtnColor} forState:UIControlStateSelected];

    UINavigationController *n4 = [[UINavigationController alloc]initWithRootViewController:c4];

    

    tb.viewControllers=@[n1,n2,n3,n4];

    

    //4.设置Window为主窗口并显示出来

    [self.window makeKeyAndVisible];

 

posted @ 2017-09-11 17:31  Nelsen_Chen  阅读(809)  评论(0)    收藏  举报