一、设置根控制器为TabbarViewController
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
MainTabbarViewController *MainTab = [[MainTabbarViewControlleralloc] init];
self.window.rootViewController = MainTab;
self.window.backgroundColor = [UIColorredColor];
[self.windowmakeKeyAndVisible];
二、在根控制器中设置每个控制器的属性
1.这里提取两个方法,一个是添加所有子控制器的方法,另一个是添加每一个子控制器的方法
- (void)setUpAllChildViewController{
// 1.添加第一个控制器
MainViewController *oneVC = [[MainViewControlleralloc]init];
[selfsetUpOneChildViewController:oneVCimage:[UIImageimageNamed:@"tab_home_icon"]title:@"首页"];
// 2.添加第2个控制器
ContactsViewController *twoVC = [[ContactsViewControlleralloc]init];
[selfsetUpOneChildViewController:twoVCimage:[UIImageimageNamed:@"js"]title:@"通讯"];
// 3.添加第3个控制器
FindViewController *threeVC = [[FindViewControlleralloc]init];
[selfsetUpOneChildViewController:threeVCimage:[UIImageimageNamed:@"qw"]title:@"发现"];
// 4.添加第4个控制器
MineViewController *fourVC = [[MineViewControlleralloc]init];
[selfsetUpOneChildViewController:fourVCimage:[UIImageimageNamed:@"user"]title:@"我的"];
}
/**
* 添加一个子控制器的方法
*/
- (void)setUpOneChildViewController:(UIViewController *)viewController image:(UIImage *)image title:(NSString *)title{
UINavigationController *navC = [[UINavigationControlleralloc]initWithRootViewController:viewController];
navC.title = title;
navC.tabBarItem.image = image;
[navC.navigationBarsetBackgroundImage:[UIImageimageNamed:@"commentary_num_bg"]forBarMetrics:UIBarMetricsDefault];
viewController.navigationItem.title = title;
[selfaddChildViewController:navC];
}
放上github链接:https://github.com/daniel1214/OutshotTabbarDemo.git