效果图(暂定)
1.配置AppIcon和LaunchImage
2.项目框架初建
需求:
多视图控制器
思路:
(1) 自定义一个继承UITabBarController的类作为window的rootViewController
(2) 给标签控制器上的每个子控制器包上一个根控制器
WBTabBarController.m
- (void)viewDidLoad {
[super viewDidLoad];
UITableViewController *homeVC = [self creatChildControllerWithClassName:@"WBHomeTableViewController" andTitle:@"首页" andImageName:@"tabbar_home"];
UITableViewController *findVC = [self creatChildControllerWithClassName:@"WBFindTableViewController" andTitle:@"发现" andImageName:@"tabbar_discover"];
UITableViewController *messageVC = [self creatChildControllerWithClassName:@"WBMessageTableViewController" andTitle:@"消息" andImageName:@"tabbar_message_center"];
UITableViewController *mineVC = [self creatChildControllerWithClassName:@"WBMineTableViewController" andTitle:@"我" andImageName:@"tabbar_profile"];
self.viewControllers = @[homeVC,messageVC,findVC,mineVC];
self.tabBar.tintColor = [UIColor orangeColor];
// KVC替换系统的tabBar
WBTabBar *tabBar = [[WBTabBar alloc] init];
tabBar.delegate =self;
[self setValue:tabBar forKey:@"tabBar"];
}
/**
创建标签栏子控制器
@param className 类名
@param title 标签标题
@param imageName 图标名称
@return 子控制器
*/
- (UITableViewController *)creatChildControllerWithClassName:(NSString *)className andTitle: (NSString *)title andImageName: (NSString *)imageName{
Class cla = NSClassFromString(className);
UITableViewController *vc = [[cla alloc] init];
// 标签名
vc.tabBarItem.title = title;
// 文字样式
NSMutableDictionary *titleAttrs = [NSMutableDictionary dictionary];
titleAttrs[NSForegroundColorAttributeName] = HWColor(123, 123, 123);
NSMutableDictionary *selectorTitleAttrs = [NSMutableDictionary dictionary];
selectorTitleAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[vc.tabBarItem setTitleTextAttributes:titleAttrs forState:UIControlStateNormal];
[vc.tabBarItem setTitleTextAttributes:selectorTitleAttrs forState:UIControlStateSelected];
// 图标
vc.tabBarItem.image = [UIImage imageNamed:imageName];
NSString *selectedImageName = [imageName stringByAppendingString:@"_selected"];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
WBNavigationController *nav = [[WBNavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
return nav;
}
WBNavigationController.m
这里用到一个单独抽取的UIBarButtonItem+Extension类,后面有写
+(void)initialize{
// 设置统一的item样式
UIBarButtonItem *item = [UIBarButtonItem appearance];
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
// 设置不可用状态
NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count > 0) {
// 跳转后不是子控制器,则自动隐藏tabbar
viewController.hidesBottomBarWhenPushed = YES;
/* 设置导航栏上面的内容 */
// 设置左边的返回按钮
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" andTarget:self andSelector:@selector(back)];
// 设置右边的更多按钮
viewController.navigationItem