UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首頁";
vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
vc1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
給tabBarItem
設(shè)置好文字和圖片后發(fā)現(xiàn)選中狀態(tài)圖片和文字都是藍(lán)色的错妖,并不是我們設(shè)置好的圖片樣式。那么如何修改圖片文字選中變藍(lán)以及文字大小呢疚沐?
修改圖片變藍(lán)
1.第一種方法暂氯,用代碼修改
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首頁";
vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
//設(shè)置圖片的渲染模式為原圖original,然后在賦給tabBarItem
UIImage *image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc1.tabBarItem.selectedImage = image;
2.第二種
DB926635-14C1-4395-8BCB-7A7658902F12.png
修改文字屬性
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
//顏色屬性
attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
//字體大小屬性
//還有一些其他屬性的key可以去NSAttributedString.h文件里去找
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:13];
NSMutableDictionary *selectAttri = [NSMutableDictionary dictionary];
selectAttri[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
selectAttri[NSFontAttributeName] = [UIFont systemFontOfSize:13];
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首頁";
//設(shè)置為選中狀態(tài)的文字屬性
[vc1.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
//設(shè)置選中狀態(tài)的屬性
[vc1.tabBarItem setTitleTextAttributes:selectAttri forState:UIControlStateSelected];
vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
vc1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
補充
進(jìn)入setTitleTextAttributes
這個方法里可以看到這個方法后面有一個宏UI_APPEARANCE_SELECTOR
,只要后面有這個宏的方法都可以通過appearance統(tǒng)一設(shè)置亮蛔。
就拿上面這個例子來說吧痴施,我們可以通過appearance統(tǒng)一對文字的屬性進(jìn)行設(shè)置,這樣就不用對每個vc的tabBarItem
設(shè)置文字的屬性了尔邓。
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:13];
NSMutableDictionary *selectAttri = [NSMutableDictionary dictionary];
selectAttri[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
selectAttri[NSFontAttributeName] = [UIFont systemFontOfSize:13];
//通過appearance對tabBarItem的文字屬性進(jìn)行統(tǒng)一設(shè)置晾剖,這樣所有的控制的tabBarItem的文字屬性久都是這種樣式的了
UITabBarItem *tabbar = [UITabBarItem appearance];
[tabbar setTitleTextAttributes:attributes forState:UIControlStateNormal];
[tabbar setTitleTextAttributes:selectAttri forState:UIControlStateSelected];