#import "ZGKTabBarController.h"
@interface ZGKTabBarController ()
@end
@implementation ZGKTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
/**** 设置所有UITabBarItem的文字属性 ****/
[self setupTitleAttributes];
/**** 添加子控制器 ****/
[self setupChildViewControllers];
}
/**** 设置所有UITabBarItem的文字属性 ****/
- (void)setupTitleAttributes {
// 避免每个tabBarController的子控制器都设置文字格式所以使用appearance
/**** 设置ZGKTabBarController下所有UITabBarItem的文字属性 ****/
UITabBarItem *item = [UITabBarItem appearance];
/**** 普通状态下的文字属性 ****/
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 字体大小
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; // 文字颜色
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
/**** 选中状态下的文字属性 ****/
NSMutableDictionary *selectedmalAttrs = [NSMutableDictionary dictionary];
selectedmalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 字体大小
selectedmalAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; // 文字颜色
[item setTitleTextAttributes:selectedmalAttrs forState:UIControlStateSelected];
}
/**** 添加子控制器 ****/
- (void)setupChildViewControllers {
// 精华
[self setupOneChildViewController:[[UITableViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
// 新帖
[self setupOneChildViewController:[[UITableViewController alloc] init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
// 关注
[self setupOneChildViewController:[[UIViewController alloc] init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
// 我的
[self setupOneChildViewController:[[UITableViewController alloc] init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
}
/**** 初始化一个子控制器 ****/
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName {
// 1.设置子控制器的背景颜色
vc.view.backgroundColor = [UIColor purpleColor];
// 2.设置tabBarItem的标题
vc.tabBarItem.title = title;
// 上面设置了UITabBarItem *item = [UITabBarItem appearance],所以就不用在这里逐一设置了
// [vc.tabBarItem setTitleTextAttributes:<#(nullable NSDictionary<NSString *,id> *)#> forState:<#(UIControlState)#>]
// 3.设置tabBarItem的图片
vc.tabBarItem.image = [UIImage imageNamed:imageName];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
// 4.添加到tabBar的子控制器中
[self addChildViewController:vc];
}
/**
* 初始化一个子控制器
*
* @param clazz 子控制器类名
* @param title 标题
* @param image 图标
* @param selectedImage 选中的图标
*/
//- (void)setupOneChildViewController:(Class)clazz title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
//{
// 可能不是通过alloc和init创建的,所以最好不要传入class,直接传控制器进来
// UIViewController *vc = [[clazz alloc] init];
// vc.view.backgroundColor = [UIColor redColor];
// vc.tabBarItem.title = title;
// vc.tabBarItem.image = [UIImage imageNamed:image];
// vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
// [self addChildViewController:vc];
//}
@end
总结:
- appearance的使用使得tabBarItem的文字格式只设置一次, 不用在每个子控制器中重复设置, 减少代码量. 但也可以通过抽取方法, 统一设置, 如:[self setupChildViewControllers]方法中的[vc.tabBarItem setTitleTextAttributes:<#(nullable NSDictionary<NSString *,id> *)#> forState:<#(UIControlState)#>].
2.设置tabBarItem一般就是设置文字和图片, 具体的还可以设置文字大小颜色和背景图片, 但是要注意是分normal和selected的.
3.在tabBarController添加子控制器的时候不使用- (void)setupOneChildViewController:(Class)clazz title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage, 是因为子控制器可能不是通过alloc和init创建的, 要分类讨论, 所以最好不要传入class, 直接传控制器对象进来
4.tabBarController在添加子控制器的时候可以看出, 子控制器的tabBarItem属性可以看做是自己选择"取什么名字和穿什么衣服"来表示自己, 通过vc.tabBarItem来设置tabBarController.tabBar的内容, 而你要DIY的话, 就要自定义tabBar.
5.appearance是指拿到该"控制器"所有的"某类控件"的外观统一做处理.