/* 项目架构(结构)搭建:主流结构(UITabBarController + 导航控制器) -> 项目开发方式 1.storyboard 2.纯代码 */ @interface AppDelegate () @end @implementation AppDelegate // 程序启动的时候就会调用 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; // 2.设置窗口根控制器 UITabBarController *tabBarVc = [[UITabBarController alloc] init]; self.window.rootViewController = tabBarVc; // 2.1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构 // 精华 XMGEssenceViewController *essenceVc = [[XMGEssenceViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:essenceVc]; // initWithRootViewController:push // tabBarVc:会把第0个子控制器的view添加去 [tabBarVc addChildViewController:nav]; // 新帖 XMGNewViewController *newVc = [[XMGNewViewController alloc] init]; UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:newVc]; // tabBarVc:会把第0个子控制器的view添加去 [tabBarVc addChildViewController:nav1]; // 发布 XMGPublishViewController *publishVc = [[XMGPublishViewController alloc] init]; // tabBarVc:会把第0个子控制器的view添加去 [tabBarVc addChildViewController:publishVc]; // 关注 XMGFriendTrendViewController *ftVc = [[XMGFriendTrendViewController alloc] init]; UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:ftVc]; // initWithRootViewController:push // tabBarVc:会把第0个子控制器的view添加去 [tabBarVc addChildViewController:nav3]; // 我 XMGMeViewController *meVc = [[XMGMeViewController alloc] init]; UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:meVc]; // initWithRootViewController:push // tabBarVc:会把第0个子控制器的view添加去 [tabBarVc addChildViewController:nav4]; // 2.2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性 // 0:nav nav.tabBarItem.title = @"精华"; nav.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; nav.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"]; // 1:新帖 nav1.tabBarItem.title = @"新帖"; nav1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"]; nav1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"]; // 2:发布 publishVc.tabBarItem.image = [UIImage imageNamed:@"tabBar_publish_icon"]; publishVc.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_publish_click_icon"]; // 3.关注 nav3.tabBarItem.title = @"关注"; nav3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"]; nav3.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"]; // 4.我 nav4.tabBarItem.title = @"我"; nav4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"]; nav4.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"]; /* 问题: 1.选中的图片被渲染 2.选中标题颜色:黑色 标题字体大 3.发布按钮显示不出来 */ // 3.显示窗口 1.成为UIApplication主窗口 2. [self.window makeKeyAndVisible]; return YES; }
自定义tabbarController,代码调整后
/* 项目架构(结构)搭建:主流结构(UITabBarController + 导航控制器) -> 项目开发方式 1.storyboard 2.纯代码 */ @interface AppDelegate () @end @implementation AppDelegate // 自定义类:1.可以管理自己业务 // 封装:谁的事情谁管理 =. 方便以后去维护代码 // 程序启动的时候就会调用 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; // 2.设置窗口根控制器 XMGTabBarController *tabBarVc = [[XMGTabBarController alloc] init]; self.window.rootViewController = tabBarVc; /* 问题: 1.选中的图片被渲染 2.选中标题颜色:黑色 标题字体大 3.发布按钮显示不出来 */ // 3.显示窗口 1.成为UIApplication主窗口 2. [self.window makeKeyAndVisible]; return YES; }
#import "XMGTabBarController.h" #import "XMGEssenceViewController.h" #import "XMGFriendTrendViewController.h" #import "XMGMeViewController.h" #import "XMGNewViewController.h" #import "XMGPublishViewController.h" @interface XMGTabBarController () @end @implementation XMGTabBarController #pragma mark - 生命周期方法 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构 [self setupAllChildViewController]; // 2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性 [self setupAllTitleButton]; } #pragma mark - 添加所有子控制器 - (void)setupAllChildViewController { // 精华 XMGEssenceViewController *essenceVc = [[XMGEssenceViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:essenceVc]; // initWithRootViewController:push // tabBarVc:会把第0个子控制器的view添加去 [self addChildViewController:nav]; // 新帖 XMGNewViewController *newVc = [[XMGNewViewController alloc] init]; UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:newVc]; // tabBarVc:会把第0个子控制器的view添加去 [self addChildViewController:nav1]; // 发布 XMGPublishViewController *publishVc = [[XMGPublishViewController alloc] init]; // tabBarVc:会把第0个子控制器的view添加去 [self addChildViewController:publishVc]; // 关注 XMGFriendTrendViewController *ftVc = [[XMGFriendTrendViewController alloc] init]; UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:ftVc]; // initWithRootViewController:push // tabBarVc:会把第0个子控制器的view添加去 [self addChildViewController:nav3]; // 我 XMGMeViewController *meVc = [[XMGMeViewController alloc] init]; UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:meVc]; // initWithRootViewController:push // tabBarVc:会把第0个子控制器的view添加去 [self addChildViewController:nav4]; } // 设置tabBar上所有按钮内容 - (void)setupAllTitleButton { // 0:nav UINavigationController *nav = self.childViewControllers[0]; nav.tabBarItem.title = @"精华"; nav.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; nav.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"]; // 1:新帖 UINavigationController *nav1 = self.childViewControllers[1]; nav1.tabBarItem.title = @"新帖"; nav1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"]; nav1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"]; // 2:发布 XMGPublishViewController *publishVc = self.childViewControllers[2]; publishVc.tabBarItem.image = [UIImage imageNamed:@"tabBar_publish_icon"]; publishVc.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_publish_click_icon"]; // 3.关注 UINavigationController *nav3 = self.childViewControllers[3]; nav3.tabBarItem.title = @"关注"; nav3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"]; nav3.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"]; // 4.我 UINavigationController *nav4 = self.childViewControllers[4]; nav4.tabBarItem.title = @"我"; nav4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"]; nav4.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end