您的位置:首页 > 产品设计 > UI/UE

iOS7修改UITabBar文本颜色以及解决自定义选中图片显示为默认蓝色的问题

2015-05-22 10:30 856 查看
修改UITabbar背景色和文本颜色和大小:

[[UITabBar appearance] setBackgroundColor:UIColorFromRGB(0xeff3f4)];
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:12.5f],
NSForegroundColorAttributeName : UIColorFromRGB(0x929292)
} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName : UIColorFromRGB(0xFF87C332)
} forState:UIControlStateSelected];

解决自定义选中图片显示为默认蓝色的问题:

在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 *iconImage = [UIImage imageNamed:@"icon.png"];
UIImage *iconImageSel = [UIImage imageNamed:@"icon_selected.png"];

iconImage = [iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
iconImageSel = [iconImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image: iconImage selectedImage: iconImageSel];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐