您的位置:首页 > 移动开发

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

2014-11-19 17:18 776 查看
场景:

在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:<span style="color:#FF0000;">GET_IOS_VERSION >= 8.0?[[UIImage imageNamed: @"tab_xxx_focus"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]:<pre name="code" class="objc">[UIImage imageNamed: @"tab_xxx_focus"]</span> 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"]];
只要将每个<pre name="code" class="objc">setFinishedSelectedImage
方的item项[UIImage imageNamed: @"tab_xxx_focus"]代换成这句判断一下系统版本就可以了

GET_IOS_VERSION >= 8.0?[[UIImage imageNamed: @"tab_xxx_focus"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]:<span style="color:#FF0000;"></span><pre name="code" class="objc">[UIImage imageNamed: @"tab_xxx_focus"]





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

问题图片如下:



正常图片如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐