UITabBarItem与UIImageRenderingMode

来源:互联网 发布:龙之信条身材捏脸数据 编辑:程序博客网 时间:2024/06/06 03:50

前言

今天遇到了UITabBarItem中图片错误的信息。
这是解决问题之后整理下来的心得。

创建UITabBarIte

在创建UITabBarItem的时候一般会为创建的UITabBarItem赋值三个东西。 标题(title),图片(image),选中时显示的图片(selectedImage)。

tabBarItem.title = @"Two";  tabBarItem.image = [UIImage imageNamed:@"one.png"];  tabBarItem.selectedImage = [UIImage imageNamed:@"two.png"];

现在的IOS7中新增了方法,

 - (instancetype)initWithTitle:(nullableNSString *)title image:(nullableUIImage *)image selectedImage:(nullableUIImage *)selectedImage NS_AVAILABLE_IOS(7_0);

使新建tabBarItem更加方便。现在可以将上面三行代码改为:

 tabBarItem= [[UITabBarItem alloc]initWithTitle:@"Two"                image:[UIImage imageNamed:@"one.png"]selectedImage:[UIImageimageNamed:@"two.png"]];

在该方法替代了从IOS5产生到IOS7开始抛弃的三个方法:

 - (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal"); (nullable UIImage *)finishedSelectedImage  NS_DEPRECATED_IOS(5_0,7_0); (nullable UIImage *)finishedUnselectedImage  NS_DEPRECATED_IOS(5_0,7_0);

同时在相同的API中,有这么一个注释:

/ The unselected image is autogenerated from the image argument. The selected image is autogenerated from the selectedImage if provided and the image argument otherwise.
To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h)
/

最后一句粗体文本意思是为了防止系统渲染,必须以UIImageRenderingModeAlwaysOriginal提供图片。若不以这种形式,那么提供的图片显示时会发生渲染错误的问题。
那么UIImageRenderingModeAlwaysOriginal到底是什么呢?


UIImageRenderingMode

在IOS7中增加创建UITabBarItem的方法的同时新增了 UIImageRenderingMode属性。

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    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information } NS_ENUM_AVAILABLE_IOS(7_0);

在该模式的枚举值中,一共存在三个值,
UIImageRenderingModeAutomatic、 UIImageRenderingModeAlwaysOriginal与UIImageRenderingModeAlwaysTemplate。

1.UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
2.UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
3.UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。

UIImageRenderingMode属性的默认值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取决于它显示的位置。其他情况可以看下面的图例


图例

最后

修改上面的代码,将两者结合到一起
UIImageRenderingMode使用:

  UIImage *img = [UIImage imageNamed:@"test.png"];  img=[img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

综合:

 tabBarItem= [[UITabBarItemalloc]initWithTitle:@"Five"                     image:[[UIImageimageNamed:@"five.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]                                   selectedImage:[[UIImageimageNamed:@"five.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]];
原创粉丝点击