美文网首页
用navgationController和tabbarViewC

用navgationController和tabbarViewC

作者: fly大梦想家 | 来源:发表于2017-07-27 11:11 被阅读32次

    多个子vc公有一个navgationController,一个tabbarController

    1.自定义个navgationController,是window的rootVC,navgationVC的rootVC是TabVC

    // 在navigationController.m中
    - (void)setRootViewControllerWithNavigation{
        _tab = [[UITabBarController alloc]init];
        
        MainViewController *mainViewController = [MainViewController new];
     
        NewsViewController *activeViewController = [NewsViewController new];
      
        ManagerViewController *managerViewController = [ManagerViewController new];
    
        StoreViewController *storeViewController = [StoreViewController new];
       
        MineViewController *mineViewController = [MineViewController new];
      
        mainViewController.title = @"首页";
        activeViewController.title = @"资讯";
        managerViewController.title = @"游戏管理";
        storeViewController.title = @"商城";
        mineViewController.title = @"我的";
        _tab.viewControllers = @[mainViewController,activeViewController,storeViewController,mineViewController];
        self.navigationController = [[BLNavigationController alloc]initWithRootViewController:_tab];
        
        self.appWindow.rootViewController = self.navigationController;
    }
    // 在applegate中调用
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
        [[BLViewControllerManager sharedManager] setRootViewControllerWithNavigation];
        return YES;
    }
    

    2.不需要navgationBar的在vc的viewWillAppear方法中隐藏,隐藏后所有vc的navgationBar都会被隐藏,所以需要的vc中的viewWillAppear中设置为NO;其他属性也可以在该方法中区分

    - (void)viewWillAppear:(BOOL)animated{
        self.navigationController.navigationBarHidden = YES;
    }
    

    3.给vc设置navgationItem标题时按普通方法无法显示标题
    self.navigationController.navigationItem.title = @"资讯中心";
    ,所以需要这样写

    - (void)viewWillAppear:(BOOL)animated{
        self.navigationController.navigationBarHidden = NO;
        self.tabBarController.navigationItem.title = @"资讯中心";
        self.tabBarController.navigationItem.rightBarButtonItem = nil;
        self.tabBarItem.badgeValue = @"1";    // tabar上的小红点数字,类似微信未读数
    }
    
    

    每个子vc有自己的navgationController,共有一个tabbarController

    1 . 自定义TabbarVC,是window的rootVC,TabbarVC的viewcontrollers是navgationVC数组

    // 在自定义的TabbarVC中
    -(void)createTabBar{
        NewsViewController * news = [[NewsViewController alloc] init];
        HeroViewController * hero = [[HeroViewController alloc] init];
        FindViewController * find = [[FindViewController alloc] init];
        MEViewController * me = [[MEViewController alloc] init];
        
        NSMutableArray * array = [NSMutableArray arrayWithObjects:news,hero,find,me, nil]; // 不要用不可变的给可变的赋值
        NSArray * titleArr = @[@"新闻",@"英雄",@"发现",@"我"];
        NSArray * normalArr = @[@"tab_icon_news_normal@2x",@"tab_icon_friend_normal@2x",@"tab_icon_quiz_normal@2x",@"tab_icon_more_normal@2x"];
        NSArray * selectedArr = @[@"tab_icon_news_press@2x",@"tab_icon_friend_press@2x",@"tab_icon_quiz_press@2x",@"tab_icon_more_press@2x"];
        // 标签栏控制器
        for (int i = 0; i< array.count; i ++) {
            // 得到每个视图控制器
            UIViewController * vc = array[i];
            // 视图控制器 --> 导航控制器
            UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
            if (i == 2) {
                nav.navigationBarHidden = YES;
            }
            // 替换数组(视图控制器 --> 导航控制器)
            [array replaceObjectAtIndex:i withObject:nav];
            // 标题
            vc.title = titleArr[i];
            // *渲染模式 : 保证显示图片与给定图片色调一致
            UIImage * normalImage = [UIImage imageNamed:normalArr[i]];
            UIImage * selectedImage = [UIImage imageNamed:selectedArr[i]];
            // 进行渲染后赋值
            nav.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            nav.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        self.viewControllers = array;
    }
    // 在applegate中调用
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
      _window.rootViewController = [[MainTabBarViewController alloc] init];
        return YES;
    }
    

    每个子VC的navgation不会互相影响都可以随便设置属性了

    相关文章

      网友评论

          本文标题:用navgationController和tabbarViewC

          本文链接:https://www.haomeiwen.com/subject/pyzakxtx.html