ios - 设置tabbarItem显示图片本来颜色,设置tabbarItem title的颜色

    HomeViewController *homevc = [[HomeViewController alloc] init];
    UINavigationController *homenav = [[UINavigationController alloc] initWithRootViewController:homevc];
    homenav.tabBarItem.title = @"首页";
    homenav.tabBarItem.image = [UIImage imageNamed:@"home"] ;

    
    CustomerViewController *custvc = [[CustomerViewController alloc] init];
    UINavigationController *custnav = [[UINavigationController alloc] initWithRootViewController:custvc];
    custnav.tabBarItem.title = @"客户";
    custnav.tabBarItem.image = [UIImage imageNamed:@"custom"];
 
    
    MyVCViewController *myvc = [[MyVCViewController alloc] init];
    UINavigationController *mynav = [[UINavigationController alloc] initWithRootViewController:myvc];
    mynav.tabBarItem.title = @"我的";
    mynav.tabBarItem.image = [UIImage imageNamed:@"mine"];
  
    
    MyVCViewController *myvc2 = [[MyVCViewController alloc] init];
    UINavigationController *mynav2 = [[UINavigationController alloc] initWithRootViewController:myvc2];
    mynav2.tabBarItem.title = @"我的";
    mynav2.tabBarItem.image = [UIImage imageNamed:@"mine"];
 
    self.viewControllers = @[homenav,custnav,mynav,mynav2];

创建了tabbar控制器,效果如图

图片本身是红色的,效果却是系统默认的蓝色

解决方法是我们需要设置图片的model

typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
    UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used  
                                            //根据图片的使用环境和所处的绘图上下文自动调整渲染模式
    
    UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template        
                                            //始终绘制图片原始状态,不使用Tint Color这就是我们要用的
    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information  
                                            //始终根据Tint Color绘制图片(颜色)显示,忽略图片的颜色信息(也就是图片原本的东西是不显示的)。
} NS_ENUM_AVAILABLE_IOS(7_0);

我们还需要设置item上文字的颜色,使用

///*可以使用NSAttributedString.h中的键在文本属性字典中指定标题的字体、文本颜色和阴影属性。
- (void)setTitleTextAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

具体的使用:


   HomeViewController *homevc = [[HomeViewController alloc] init];
    UINavigationController *homenav = [[UINavigationController alloc] initWithRootViewController:homevc];
    homenav.tabBarItem.title = @"首页";
    homenav.tabBarItem.image = [UIImage imageNamed:@"home"] ;
  
    homenav.tabBarItem.selectedImage = [[UIImage imageNamed:@"home"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

  // 设置 tabbarItem 选中状态下的文字颜色(不被系统默认渲染,显示文字自定义颜色)
    NSDictionary *dictHome = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
  [homenav.tabBarItem setTitleTextAttributes:dictHome forState:(UIControlStateSelected) ];
    

封装一下,方便使用

/**
 设置tabBarItem

 @param imageName 图片名称
 @param selectImageName 点击时候的图片名称
 @param color 文字的颜色
 @param title item文字
 @param nav items控制器
 @return 控制器
 */
-(UINavigationController *)setTabBarItemColorAndCustomImageWithImageName :(NSString *)imageName selectImageName:(NSString *)selectImageName titleColor:(UIColor *) color title:(NSString *)title Nav:(UINavigationController *)nav  {
    nav.tabBarItem.image = [UIImage imageNamed:imageName] ;
    nav.tabBarItem.title = title;
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
     NSDictionary *dictHome = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
    [nav.tabBarItem setTitleTextAttributes:dictHome forState:(UIControlStateSelected)];
    return nav;
}

生产注释格式的快捷键是command+option+\  ,记录一下

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值