1.在使用系统的tabBar的时候,无论图片和文字的颜色是什么,系统默认的都会是蓝色,这是因为默认情况下,系统是有渲染效果的,下面介绍如何去除系统默认的渲染效果:
// 添加图片之前先把图片的渲染效果给去除
UIImage * image = [UIImage imageNamed:imageName];
// imageWithRenderingMode 图片的渲染模式 UIImageRenderingModeAlwaysOriginal 保持原始
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 添加到TabbarItem上就可以了tabBarItem.selectedImage = image;
2.如何去除文字的渲染效果
第一种方法:是拿到要修改的每一个item的去修改
// 因为在设置属性时需要传入一个字典,字典里面是一个键值对,用来修改文字的颜色,所以先创建字典
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
// 字典的赋值
[dict setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName];
// 添加到Item
[tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected];
第二种方法:拿到所有的tabbarItem对象,一次性赋值,建议在系统的initialize方法里实现,这样在初始化的时候就会完成
+ (void)initialize{
UITabBarItem * item = [UITabBarItem appearance];
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[dict setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName];
[item setTitleTextAttributes:dict forState:UIControlStateSelected];
}
补充: NSForegroundColorAttributeName 是系统的一个key,用来修改对应属性名称的颜色,由于比较长,这里介绍如何得到这个属性名。首先找到系统的UIKit.framework包,打开包中的Headers文件,然后找到NSAttributedString.h头文件,打开,就能找到了