iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug

今天在UITabBarController使用自定义图片设置UITabItem时,发现一个问题:

在iOS7以上的手机中,第一个Tab的选中图一直显示的是系统默认的蓝色图,查看了一下UITabItem的头文件,发现下面的内容:

 

/* 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)
 */
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag;
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage NS_AVAILABLE_IOS(7_0);
- (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;

意思是,如果不希望使用系统颜色,需要对图片加上属性UIImageRenderingModeAlwaysOriginal

所以按此方式实验,代码如下:

 

UIImage *musicImage = [UIImage imageNamed:@"music.png"];
UIImage *musicImageSel = [UIImage imageNamed:@"musicSel.png"];

musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image:musicImage selectedImage:musicImageSel];

 

问题解决

 

你可能感兴趣的:(iOS)