多控制器使用和常见问题

多控制器常用的有两种:一种是根控制是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 = @"发现";
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容