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+  ,记录一下

人已赞赏
iOS文章

ios - 视频播放器

2020-3-2 16:58:53

iOS文章

iOS 多个网络请求成功以后,在回调给H5

2020-3-2 18:04:09

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索