多控制器常用的有两种:一种是根控制是UITabBarController,另一种是根控制器是UINavigationController。
如下图:
根控制器是tabBar
根控制器是navContr
根控制器是tabBar
这是我们常用的一种多控制器结构,设置根控制时用UITabBarController来包装,如下:
- (void)styleOne {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
MainMenuTabBarController *mainMenuTabBarVCtr = [[MainMenuTabBarController alloc] init];
self.window.rootViewController = mainMenuTabBarVCtr;
[self.window makeKeyAndVisible];
}
这个需要注意的是,当push到下一级控制器的时候,我们要隐藏底部的tabBar。为了不每个页面,都处理,所以要想统一处理的话,我们要自定义一个UINavigationController。如下:
#import "SJBaseNavCtr.h"
@interface SJBaseNavCtr ()
@end
@implementation SJBaseNavCtr
// 返回当前可视控制器的状态栏状态
- (UIStatusBarStyle)preferredStatusBarStyle {
return [self.visibleViewController preferredStatusBarStyle];
}
- (BOOL)prefersStatusBarHidden {
return [self.visibleViewController prefersStatusBarHidden];
}
- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
// 返回到根控制器的时候,不隐藏tabBar
self.hidesBottomBarWhenPushed = NO;
return [super popToRootViewControllerAnimated:animated];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 当push到下层控制器的时候,隐藏底部的tabBar
if (self.viewControllers.count) {
self.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
if (self.viewControllers.count == 2) {
self.hidesBottomBarWhenPushed = NO;
}
return [super popViewControllerAnimated:animated];
}
@end
如果,还有别的什么全局的,也可以再这里设置,比如说返回按钮,和导航的主题颜色等等。
然后底部的tabBar,我们可以用系统的,也可以用自己的。
用系统的tabBar
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
[self addChildController:[[LCNewTTBViewController alloc] init]
imageName:@"2-5首页1"
selectedImage:@"首页2_2-5" title:@"首页"];
[self addChildController:[[LCDCBNewViewController alloc] init]
imageName:@"2-5定存宝1"
selectedImage:@"定存宝2_2-5"
title:@"定存宝"];
[self addChildController:[[LCNewAccountController alloc] init]
imageName:@"2-5我的1"
selectedImage:@"我的2_2-5"
title:@"我的"];
[self addChildController:[[LCNewMoreViewController alloc] init]
imageName:@"2-5更多1"
selectedImage:@"2-5更多2"
title:@"更多"];
}
// 设置tabBarItem的主题
+ (void)initialize {
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:LCNavBottomColor];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateFocused];
}
/**
* @brief 添加子控制器
*
* @param childVC 子控制期器
* @param image 默认图片
* @param selectedImage 选中图片
* @param title 标题
*/
- (void)addChildController:(UIViewController *)childVC
imageName:(NSString *)image
selectedImage:(NSString *)selectedImage
title:(NSString *)title
{
//设置文字和图片
childVC.title = title;
childVC.tabBarItem.image = [UIImage mr_imageOriginalWithName:image];
childVC.tabBarItem.selectedImage = [UIImage mr_imageOriginalWithName:selectedImage];
// 包装一个导航控制器,添加导航控制器为tabBarController的子控制器
LCNavigationController *nav = [[LCNavigationController alloc] initWithRootViewController:childVC];
[self addChildViewController:nav];
}
用自己的tabBar
用自己的也分两种,可以是只是移除tabBar上面的子控件,然后将我们自定义的tabBarItem(也就是自定义的button)加上去:
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// 所有初始化
tabbarTitleAry = [NSArray arrayWithObjects:@"首页", @"论坛", @"发现", @"选车", @"我", nil];
tabbarTitleImageAry = [NSArray arrayWithObjects:@"tabBar_home_normal.png",@"tabBar_category_normal.png", @"tabBar_find_normal.png", @"tabBar_cart_normal.png", @"tabBar_myJD_normal.png", nil];
tabbarTitleHightImageAry = [NSArray arrayWithObjects:@"tabBar_home_press.png", @"tabBar_category_press.png", @"tabBar_find_press.png", @"tabBar_cart_press.png",@"tabBar_myJD_press.png", nil];
_eocHomePageVC = [[HomePageVCtr alloc] init];
_eocForumVC = [[ForumVCtr alloc] init];
_eocFinderVC = [[FindVCtr alloc] init];
_eocSelectCarVC = [[SelectCarVCtr alloc] init];
_eocUserInfoVC = [[UserInfoVCtr alloc] init];
[self styleOne];
// [self styleTwo];
}
self.selectedIndex = 0;
return self;
}
- (void)styleOne {
UINavigationController *navHome = [[SJBaseNavCtr alloc] initWithRootViewController:_eocHomePageVC];
UINavigationController *navForum = [[SJBaseNavCtr alloc] initWithRootViewController:_eocForumVC];
UINavigationController *navSelectCar = [[SJBaseNavCtr alloc] initWithRootViewController:_eocFinderVC];
UINavigationController *navFinder = [[SJBaseNavCtr alloc] initWithRootViewController:_eocSelectCarVC];
UINavigationController *navUserInfo = [[SJBaseNavCtr alloc] initWithRootViewController:_eocUserInfoVC];
self.viewControllers = [NSArray arrayWithObjects:navHome, navForum, navSelectCar, navFinder, navUserInfo, nil];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!_eocTabBar) {
[self createEocTabBar];
}
}
- (void)createEocTabBar {
float tabbarWidth = self.tabBar.frame.size.width;
float tabbarHeight = self.tabBar.frame.size.height;
// 创建一个和系统同样大小的tabBar
_eocTabBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tabbarWidth, tabbarHeight)];
_eocTabBar.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
// 添加下面的每个tabBarItem
float tabbarItemWidth = tabbarWidth/tabbarTitleAry.count;
for (NSInteger i = 0; i < tabbarTitleAry.count; i++){
TabBarButton *tabbarBt = [[TabBarButton alloc] init];
[tabbarBt setFrame:CGRectMake(i*tabbarItemWidth, 0, tabbarItemWidth, tabbarHeight)];
[tabbarBt addTarget:self action:@selector(selectMenuVC:) forControlEvents:UIControlEventTouchUpInside];
tabbarBt.tag = i;
[tabbarBt setBackgroundImage:[UIImage imageNamed:tabbarTitleImageAry[i]] forState:UIControlStateNormal];
[tabbarBt setBackgroundImage:[UIImage imageNamed:tabbarTitleHightImageAry[i]] forState:UIControlStateSelected];
[_eocTabBar addSubview:tabbarBt];
if (i == 0) {
_selectTabBarBtn = tabbarBt;
_selectTabBarBtn.selected = YES;
}
}
[self tabbarStyleOne];
}
- (void)selectMenuVC:(TabBarButton *)tabBarBtn {
if (self.selectedIndex == tabBarBtn.tag) {
return;
}
// 取消原来选中的,将新的选中
self.selectedIndex = tabBarBtn.tag;
_selectTabBarBtn.selected = NO;
_selectTabBarBtn = tabBarBtn;
_selectTabBarBtn.selected = YES;
}
- (void)tabbarStyleOne {
// 移除掉所有的原生视图,remove
NSArray *tabbarViewsAry = [self.tabBar subviews];
for (UIView *view in tabbarViewsAry) {
[view removeFromSuperview];
}
// 再将自己创建的tabBar添加上去
[self.tabBar addSubview:_eocTabBar];
}
也可以直接移除掉原来的tabBar,将自己的tabBar加在原来tabBar的父控件上面,但是这样的话,自定义的导航条里面,系统的hidesBottomBarWhenPushed设置就没有作用了,所以也不太好用,一般用上面那种。
- (void)tabbarStyleTwo {
// 直接移除掉整个tabBar
UIView *superView = [self.tabBar superview];
_eocTabBar.frame = self.tabBar.frame;
[self.tabBar removeFromSuperview];
[superView addSubview:_eocTabBar];
}
导航title的设置
正确的设置 self.navigationItem.title来操作(因为self.title操作会影响tabbar的item)
tabbar(记得隐藏,因为爷控制器是tabbarViewCtr(儿的所有控制器(UINavigationController)默认都能看到tabbar))
根控制器是navContr
这种我们设置根控制器的时候,需要用UINavigationController包装在最外层,如下:
- (void)styleTwo {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
MainMenuTabBarController *mainMenuTabBarVCtr = [[MainMenuTabBarController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainMenuTabBarVCtr];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
但是这个时候UINavigationController的根控制器里面,的设置需要如下:
- (void)styleTwo {
self.viewControllers = [NSArray arrayWithObjects:_eocHomePageVC, _eocForumVC, _eocFinderVC, _eocSelectCarVC, _eocUserInfoVC, nil];
}
不能再用导航控制器去包装一层了。
导航title的设置
正确的设置 self.tabBarController.title来操作(如果self.title操作会影响tabbar的item,但对nabbar没有效果,因为导航条属于爷控制器) navbar(记得刷新,共享了一个navbar)
另外,需要注意的是self.tabBarController.title的设置需要写在viewWillAppear方法中,因为当点击下面的tabBar的时候,上面的导航栏还是同一个导航条,不会进行刷新。
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
/*
如果 self.tabBarController 在一个 UITabBarController多控制器(ATabbarVCtr)里面,那么self.tabBarController.title操作对这个ATabbarVCtr的tabbarItem的title有影响
*/
self.tabBarController.title = @"发现";
// self.title = @"发现";
}
网友评论