1.設置seleectedImage和image
設置圖片.png
2.這時候帖族,會發(fā)現(xiàn)绳泉,不管你怎么弄,只會顯示顏色不會正常顯示圖片伦意,那是因為沒有設置圖片的renderingMode屬性:
UIImageRenderingModeAutomatic // 根據(jù)圖片的使用環(huán)境和所處的繪圖上下文自動調(diào)整渲染模式火窒。
UIImageRenderingModeAlwaysOriginal // 始終繪制圖片原始狀態(tài),不使用Tint Color驮肉。
UIImageRenderingModeAlwaysTemplate // 始終根據(jù)Tint Color繪制圖片熏矿,忽略圖片的顏色信息。
設置選中圖片和未選中圖片的renderingMode屬性為:
for (UITabBarItem *item in self.tabBar.items) {
item.selectedImage = [item.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//item.title
}
這樣就可以正常顯示了离钝!
- 改變文字的顏色:先點擊下圖所選位置
選中.png
然后在下圖出修改 票编,bar tint是設置這個tabbar的背景色,下面的tint就可以設置所想要字體的顏色了
設置顏色.png
補充:代碼設置tabbar item的文字顏色卵渴?
初始化視圖控件
- (void)initView {
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithObjects:Imageone, ImageAccounttwo, ImageSecuritythree ImagePromotionfour, nil];
self.selectedIndex = 2;
for (UITabBarItem *item in self.tabBar.items) {
// 設置 TabBar Item 被選中圖標的模式
item.selectedImage = [[UIImage imageNamed:[imageArray firstObject]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[imageArray removeObjectAtIndex:0];
// 設置 TabBar Item 被選中圖標的字體顏色
if (self.selectedIndex == [self.tabBar.items indexOfObject:item]) {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x549ACC), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
} else {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x999999), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}
}
}
補充 : 當點擊某個item的時候栏妖,會調(diào)用代理方法,然后再更改tabbar item 的字體顏色
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// 設置 TabBar Item 默認的字體顏色
for (UITabBarItem *item in self.tabBar.items) {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x999999), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}
// 設置 TabBar 被選中 Item 的字體顏色
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x549ACC), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}