-
代码实现
#import "ZYTabBarViewController.h"
#import "ZYEssenceViewController.h"
#import "ZYFriendTrendsViewController.h"
#import "ZYMeViewController.h"
#import "ZYNewViewController.h"
#import "ZYPublishViewController.h"
#import "UIImage+Image.h"
@interface ZYTabBarViewController()
@end
@implementation ZYTabBarViewController
/**利用富文本设置tabbar的标题颜色和尺寸*/
+ (void)load
{
// 获取哪个类中UITabBarItem
UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
// 设置按钮选中标题的颜色
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
// 设置字体尺寸--只能设置正常状态下
NSMutableDictionary *attrsNor = [[NSMutableDictionary alloc]init];
attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
}
/**封装添加子控制器*/
- (void)setUpOneChildViewController:(UIViewController *)vc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title isHasNav:(BOOL)isHasNav
{
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageOriginalWith:selectedImage];
if (isHasNav == YES) {
vc.title = title;
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
[self addChildViewController:nav];
}else{
[self addChildViewController:vc];
}
}
#pragma mark- 添加所有子控制器
- (void)setupAllChildViewController
{ // 精华
ZYEssenceViewController *easence = [[ZYEssenceViewController alloc]init];
[self setUpOneChildViewController:easence image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon" title:@"精华" isHasNav:YES];
// 新帖
ZYNewViewController *new = [[ZYNewViewController alloc]init];
[self setUpOneChildViewController:new image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_new_click_icon" title:@"新帖" isHasNav:YES];
// 发布
ZYPublishViewController *publish = [[ZYPublishViewController alloc]init];
[self setUpOneChildViewController:publish image:@"tabBar_publish_icon" selectedImage:@"tabBar_publish_click_icon" title:nil isHasNav:NO];
// 关注
ZYFriendTrendsViewController *friendTrends = [[ZYFriendTrendsViewController alloc]init];
[self setUpOneChildViewController:friendTrends image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon" title:@"关注" isHasNav:YES];
// 我的
ZYMeViewController *me = [[ZYMeViewController alloc]init];
[self setUpOneChildViewController:me image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon" title:@"我的" isHasNav:YES];
}
#pragma mark - 生命周期
- (void)viewDidLoad
{ [super viewDidLoad];
// 设置子控制器并添加按钮的内容
[self setupAllChildViewController];
}