1. 程式人生 > >修改系統TabBar上圖片和文字的顏色

修改系統TabBar上圖片和文字的顏色

1.在使用系統的tabBar的時候,無論圖片和文字的顏色是什麼,系統預設的都會是藍色,這是因為預設情況下,系統是有渲染效果的,下面介紹如何去除系統預設的渲染效果:

// 新增圖片之前先把圖片的渲染效果給去除

UIImage * image = [UIImage imageNamed:imageName];

// imageWithRenderingMode 圖片的渲染模式  UIImageRenderingModeAlwaysOriginal 保持原始

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// 新增到TabbarItem上就可以了

tabBarItem.selectedImage = image;

2.如何去除文字的渲染效果

第一種方法:是拿到要修改的每一個item的去修改

// 因為在設定屬性時需要傳入一個字典,字典裡面是一個鍵值對,用來修改文字的顏色,所以先建立字典

NSMutableDictionary * dict = [NSMutableDictionarydictionary];

// 字典的賦值

[dict setValue:[UIColorredColorforKey:NSForegroundColorAttributeName];

// 新增到Item

[tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected];

第二種方法:拿到所有的tabbarItem物件,一次性賦值,建議在系統的initialize方法裡實現,這樣在初始化的時候就會完成

+ (void)initialize{

UITabBarItem * item = [UITabBarItemappearance];

NSMutableDictionary * dict = [NSMutableDictionarydictionary];

    [dict setValue:[UIColorredColor

forKey:NSForegroundColorAttributeName];

    [item setTitleTextAttributes:dict forState:UIControlStateSelected];

}

補充:NSForegroundColorAttributeName 是系統的一個key,用來修改對應屬性名稱的顏色,由於比較長,這裡介紹如何得到這個屬性名。首先找到系統的UIKit.framework包,開啟包中的Headers檔案,然後找到NSAttributedString.h標頭檔案,開啟,就能找到了