UITabbar的简单操作和实际应用

 简易编辑Tabbar

    //**标签栏控制器的初始化

    UITabBarController * tabbarC = [[UITabBarController alloc] init];


    //设置tabBar的颜色

    tabbarC.tabBar.barTintColor = [UIColor brownColor];

     //设置按钮颜色

    tabbarC.tabBar.tintColor = [UIColor cyanColor];   

    //**创建视图控制器,类似ViewController

    UIViewController * c1 = [[UIViewController alloc] init];

    c1.view.backgroundColor = [UIColor greenColor];   

    //**创建系统自带的UITabBarItem

    UITabBarItem * c1Item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0];

    //另外两种种初始化方法

    UITabBarItem * c2Item = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"tab_3"] selectedImage:selectedImage2];

    UITabBarItem * c3Item = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tab_0"] tag:0];

    //设置右上角红色小气泡

    c1Item.badgeValue = @"100";

    //**创建好的item赋值给tabBarItem

    c1.tabBarItem = c1Item;

    //第一种方式添加子控制器

   [tabbar addChildViewController:c1];

    [tabbar addChildViewController:c2];

    [tabbar addChildViewController:c3];

    //**第二种方式添加子控制器

    tabbarC.viewControllers = @[c1,c2,c3,c4,c5,c6];

    //**设置主控制器

    self.window.rootViewController = tabbarC;

    //**更改显示先后顺序

    [self.window makeKeyAndVisible];

    //更改选中状态图片UIImageRenderingModeAlwaysOriginal 不做任何修改的图片

    self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_0"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Nav + Tabbar 
 
重要:
1、实现UIviewcontroller(继承于此类)的实例化
2、实现UINavigationController(继承于此类) 的实例化
3、将viewcontroller 传给navigationcontroller
4、实现UITabBarController(继承于此类) 的实例化
5、将navigationcontroller传给tabBarController
6、self.window.rootViewController

    //当导航视图控制器压栈时,隐藏tabbar

    vc.hidesBottomBarWhenPushed = YES;

1、简单易懂

     UITabBarController * tabbarVC = [[UITabBarController alloc] init];     

     FirstViewController * firstVC = [[FirstViewController alloc] init];

     firstVC.title = @"第一页";

     UINavigationController * firstNav = [[UINavigationController alloc] initWithRootViewController:firstVC];

     SecondViewController * secondVC = [[SecondViewController alloc] init];

     secondVC.title = @"第二页";

     UINavigationController * secondNav = [[UINavigationController alloc] initWithRootViewController:secondVC];

     ThirdViewController * thirdVC = [[ThirdViewController alloc] init];

     thirdVC.title = @"第三页";

     UINavigationController * thirdNav = [[UINavigationController alloc] initWithRootViewController:thirdVC];

     tabbarVC.viewControllers = @[firstNav,secondNav,thirdNav];

     tabbarVC.viewControllers = @[firstVC,secondVC,thirdVC];

     self.window.rootViewController = tabbarVC;

2、for循环遍历

//以视图控制器的名字创建字符串数组

     NSMutableArray * viewControllerNames = [NSMutableArray arrayWithArray:@[@"FirstViewController",@"SecondViewController",@"ThirdViewController"]];

     //以视图控制器的标题创建字符串数组

     NSArray * titleNames = @[@"第一页",@"第二页",@"第三页"];

     //遍历viewControllerNames数组

     for (NSInteger i = 0; i < viewControllerNames.count; i++) {

     //获取数组中的每一项

     NSString * vcName = viewControllerNames[i];//FirstViewController

     //将字符串转换为Class类型,并通过Class初始化对象

     UIViewController * vc = [[NSClassFromString(vcName) alloc] init];

     //设置题目

     vc.title = titleNames[i];

     //创建导航控制器

     UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];

     //nav替换viewControllerNames数组中的字符串

     [viewControllerNames replaceObjectAtIndex:i withObject:nav];

     }     

     UITabBarController * tabbarVC = [[UITabBarController alloc] init];

     tabbarVC.viewControllers = viewControllerNames;

     self.window.rootViewController = tabbarVC;

3、自定义   

    //以视图控制器的名字创建字符串数组

    NSMutableArray * viewControllerNames = [NSMutableArray arrayWithArray:@[@"FirstViewController",@"SecondViewController",@"ThirdViewController"]];

    //以视图控制器的标题创建字符串数组

    NSArray * titleNames = @[@"第一页",@"第二页",@"第三页"];

    //遍历viewControllerNames数组

    for (NSInteger i = 0; i < viewControllerNames.count; i++) {

        //获取数组中的每一项

        NSString * vcName = viewControllerNames[i];//FirstViewController

        //将字符串转换为Class类型,并通过Class初始化对象

        UIViewController * vc = [[NSClassFromString(vcName) alloc] init];

        //设置题目

        vc.title = titleNames[i];

        //创建导航控制器

        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];

        //nav替换viewControllerNames数组中的字符串

        [viewControllerNames replaceObjectAtIndex:i withObject:nav];

    }

    self.viewControllers = viewControllerNames;

原文地址:https://www.cnblogs.com/PSSSCode/p/5271799.html