以前寫tabbar都是在AppDelegate.m里面寫,這次看到別人將UITabBarController自定義翁狐,于是心血來潮办铡,想著這是一種新的寫法,就想試試百框,下面將寫的過程中遇到的一些困惑和神坑記錄下來闲礼。
1.新建一個UITabBarController的類,在.m文件里統(tǒng)一給TabbarItem設置屬性铐维,文字的大小柬泽,被選中的顏色等
/**
* 設置item屬性
*/
- (void)setupItem
{
// UIControlStateNormal狀態(tài)下的文字屬性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
// 文字顏色
normalAttrs[NSForegroundColorAttributeName] = RGB(170, 170, 170);
// 文字大小
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:11];
// UIControlStateSelected狀態(tài)下的文字屬性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
// 文字顏色
selectedAttrs[NSFontAttributeName] = normalAttrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = RGB(249, 103, 80);
// 統(tǒng)一給所有的UITabBarItem設置文字屬性
// 只有后面帶有UI_APPEARANCE_SELECTOR的屬性或方法, 才可以通過appearance對象來統(tǒng)一設置
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
2.自定義UITabBarController,設置其標題文字嫁蛇,默認的圖片和被選中的圖片
-(void)setupChildViewController:(UIViewController *)childController
title:(NSString *)title
image:(NSString *)image
selectedImage:(NSString *)selectedImage
{
childController.title = title;
childController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//[childController.tabBarItem setImage:[UIImage imageNamed:image]];
//[childController.tabBarItem setSelectedImage:[UIImage imageNamed:selectedImage]];
XCFNavViewController *nav = [[XCFNavViewController alloc] initWithRootViewController:childController];
nav.title = title;
[self addChildViewController:nav];
}
實現(xiàn)這個方法
-(void)createChildTabBar
{
[self setupChildViewController:[[XCFKitchenViewController alloc] init]
title:@"下廚房"
image:@"tabADeselected"
selectedImage:@"tabASelected"];
[self setupChildViewController:[[XCFBazaarController alloc] init]
title:@"市集"
image:@"tabBDeselected"
selectedImage:@"tabBSelected"];
[self setupChildViewController:[[XCFCommunityViewController alloc] init]
title:@"社區(qū)"
image:@"tabCDeselected"
selectedImage:@"tabCSelected"];
[self setupChildViewController:[[XCFMeController alloc] init]
title:@"我"
image:@"tabDDeselected"
selectedImage:@"tabDSelected"];
}
就這樣锨并,一個自定義的tabbar就基本寫成了。最后睬棚,需要在AppDelegate.m里面設置一下rootcontrller第煮,
self.window.rootViewController = [[XCFTabBarController alloc] init];
就這樣,一個大致的模子就出來了闸拿,下面上圖
Paste_Image.png
下面需要注意的兩個問題空盼,第一個問題是,一定要將圖片設置正確新荤,不然會出現(xiàn)
下圖所示的情況
Paste_Image.png
會出現(xiàn)一個很大很難看的圖標揽趾,出現(xiàn)這個的原因是圖片的格式?jīng)]有設置對,一定要將圖片的格式設置為@2x苛骨,這樣才可以篱瞎。
第二個問題是ios7以后,如果自定義的話痒芝,需要設置圖片的渲染模式俐筋,不然是不會顯示原圖的,蘋果默認會顯示藍色的严衬,如下圖所示
Paste_Image.png
出現(xiàn)了這個澄者,就是因為沒有選中圖片的渲染模式,蘋果默認就顯示藍色了请琳,這里我們要設置圖片不渲染粱挡,也就是始終保持原來的樣子。
childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
這樣就可以了俄精。