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 ]];
- UITabBarItem与UIImageRenderingMode
- UITabBarItem的终极写法(UIImageRenderingMode)的使用
- UITabBarItem
- UITabBarItem
- iOS UIImageRenderingMode
- UIImageRenderingMode使用介绍
- ios 修改UITabBarItem字体颜色,正常与选中状态
- UITabBarItem 自定义
- 关于UITabbarItem定制选中与未选中两张图片的问题
- 通过UIImageRenderingMode改变工程图片颜色
- ios 故事版 设定UIImageRenderingMode的方式
- 关于uitabbaritem图片显示
- UITabBarItem修改背景
- UITabBarItem文字的设置
- UIBarItem UITabBarItem常用方法
- 改变UITabBarItem 字体颜色
- UIBarItem UITabBarItem常用方法
- iOS ---UITabBarItem 选中图
- 智能手机双摄像头产业链、工艺、及原理
- VB 学习整理1 基本概念 数据类型
- Python高级编程——12.系统编程(多进程和多线程)总述
- Oracle归档日志写满(ORA-00257错误)
- Ubuntu12.04NFS挂载根文件系统
- UITabBarItem与UIImageRenderingMode
- Selenium + Pycharm+python3.6自动化环境搭建
- fragment遇到getactivity为空的情况
- Pb(100) The method getType() is undefined for the type new TypeToken<List<String>>(){}
- 获取当前年份后两位
- GCC基础
- 进制转换
- 『MACHINE LEARNING』读书笔记|周志华《机器学习》|5.2神经网络感知机
- 使用nat123实现外网访问局域网中的linux主机