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


修改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];




你可能感兴趣的:(iOS)