本文共 2443 字,大约阅读时间需要 8 分钟。
/** * 初始化所有的子控制器 */- (void)setupAllChildViewControllers{ UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; GFBHomeVC *firstViewController = [sb instantiateViewControllerWithIdentifier:@"GFBHomeVC"]; [self setupChildViewController:firstViewController title:@"首页" tabBarTitle:@"首页" imageName:@"icon_home_normal" selectedImageName:@"icon_home_selected"]; GFBTypesVC *secondViewController = [sb instantiateViewControllerWithIdentifier:@"GFBTypesVC"]; [self setupChildViewController:secondViewController title:@"分类" tabBarTitle:@"分类" imageName:@"iocn_classification_normal" selectedImageName:@"iocn_classification_selected"]; GoodsListVC *thirdViewController = [[GoodsListVC alloc] init]; [self setupChildViewController:thirdViewController title:@"购物车" tabBarTitle:@"购物车" imageName:@"icon_shopping-cart_normal" selectedImageName:@"icon_shopping-cart_selected"]; GFBMineVC *fourthViewController = [sb instantiateViewControllerWithIdentifier:@"GFBMineVC"]; [self setupChildViewController:fourthViewController title:@"个人中心" tabBarTitle:@"个人中心" imageName:@"icon_personal-center_normal" selectedImageName:@"icon_personal-center_selected"];}/** * 初始化一个子控制器 * * @param childVc 需要初始化的子控制器 * @param title 标题 * @param imageName 图标 * @param selectedImageName 选中的图标 */- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title tabBarTitle:(NSString *)tabBarTitle imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{ // 1.设置控制器的属性 childVc.navigationItem.title = title; // 设置图标 childVc.tabBarItem.image = [UIImage imageNamed:imageName]; childVc.tabBarItem.title = tabBarTitle; // 设置选中的图标 UIImage *selectedImage = [UIImage imageNamed:selectedImageName]; childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *normalImage = [UIImage imageNamed:imageName]; childVc.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UINavigationController *nav; if([childVc isKindOfClass:[GFBMineVC class]]) { nav = [[KLTNavigationController alloc]initWithRootViewController:childVc]; }else { // 2.包装一个导航控制器 nav = [[GFBBaseNaVC alloc] initWithRootViewController:childVc]; } [self addChildViewController:nav]; }复制代码
转载地址:http://zghno.baihongyu.com/