解决iOS8以上系统下app底部UITabBarItem上自定义的背景色和图片,在首次启动或push之后再pop回去会变成系统默认颜色-蓝色问题

场景:
        在ios8以下系统下正常显示,在ios8以上系统底部UITabBarItem会变成默认蓝色,遮挡图片上的字,

首先,在ios8以下没问题是因为在iOS8以下没问题是以下方法对ios8以下的系统支持比较好:

- (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");
该方法的说明仅支持5, 7的系统;

兼容ios5以上系统修改后的代码如下:

UITabBarItem *tabBarItemMyGroup = [[UITabBarItem alloc]
                                       initWithTitle:@"名称1"
                                       image:[UIImage imageNamed:@"tab_xxx_unfocus"]
                                       selectedImage:[UIImage imageNamed:@"tab_xxx_focus"]];
        
        [tabBarItemMyGroup setFinishedSelectedImage:GET_IOS_VERSION >= 8.0?[[UIImage imageNamed: @"tab_xxx_focus"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]:
[UIImage imageNamed: @"tab_xxx_focus"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_mybang_unfocus"]];
        [tabBarItemMyGroup setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor colorWithRed:(152.0/255.0f) green:(154.0/255.0f) blue:(158.0/255.0f) alpha:(1.0f)], UITextAttributeTextColor, nil]
                                         forState:UIControlStateNormal];
        [tabBarItemMyGroup setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   color, UITextAttributeTextColor,
                                                   nil] forState:UIControlStateSelected];
        myNavVC.tabBarItem = tabBarItemMyGroup;
 
  


ios8下问题代码出现在:

[tabBarItemMyGroup setFinishedSelectedImage:[UIImage imageNamed: @"tab_xxx_focus"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_mybang_unfocus"]];

只要将每个
setFinishedSelectedImage
方的item项[UIImage imageNamed: @"tab_xxx_focus"]代换成这句判断一下系统版本就可以了
GET_IOS_VERSION >= 8.0?[[UIImage imageNamed: @"tab_xxx_focus"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]:
[UIImage imageNamed: @"tab_xxx_focus"]
 
  

 
  

当setFinishedSelectedImage时始终选择最原始用户自己指定的图片利用UIImage的imageWithRenderingMode来处理一下

问题图片如下:


正常图片如下:



你可能感兴趣的:(objective-c移动开发)