TabBarItem的图片默认颜色 让我甚是头痛,美工MM说灰色的好丑 T_T
1.iOS7以下
查阅了相关文档,这样一个方法
/* These methods are now deprecated. Please use -initWithTitle:image:selectedImage:. */- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal");
不过你也看到了,这iOS7开始就弃用了,但至少还能用,如果不介意那烦人的警告的话;
2.iOS7以上
那警告实在闹心,根据警告内容,我又查了资料,结果比较有趣
//UITabBar设置未选中图片[yourTabBarItem setImage:[[UIImage imageNamed:@"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];//UITabBar设置选中图片[yourTabBarItem setSelectedImage:[[UIImage imageNamed:@"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
有一个共同点就是图片多调用了
// Create a version of this image with the specified rendering mode. By default, images have a rendering mode of UIImageRenderingModeAutomatic.- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
也就是设置了UIImageRenderingMode,下面是枚举
它使用UIImageRenderingMode枚举值来设置图片的renderingMode属性。该枚举中包含下列值:UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。 UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。 UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
3.以上只就解决了图片颜色问题还有文字 颜色还是默认的颜色
//设置未选中字体颜色 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0x003500), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; //设置选中字体颜色 [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: UIColorFromRGB(0xff3500), NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
附上一个颜色宏
/** Create a UIColor from a hex value. For example, `UIColorFromRGB(0xFF0000)` creates a `UIColor` object representing the color red. */#define UIColorFromRGB(rgbValue) \[UIColor \colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \blue:((float)(rgbValue & 0x0000FF))/255.0 \alpha:1.0]
参考: