UITabBarController

  • UITabBarController的使用步骤

    • 初始化UITabBarController
    • 设置UIWindow的rootViewController为UITabBarController
    • 根据具体情况,通过addChildViewController方法添加对应个数的子控制器
  • UITabBarController添加控制器的方式有2种 添加单个子控制器

    -(void)addChildViewController:(UIViewController *)childController;
    
  • 设置子控制器数组
    @property(nonatomic,copy) NSArray *viewControllers;
    
  • UITabBarItem有以下属性影响着UITabBarButton的内容

    • 标题文字
      @property(nonatomic,copy) NSString *title;
      
    • 图标

      @property(nonatomic,retain) UIImage *image;
      
    • 选中时的图标

      @property(nonatomic,retain) UIImage *selectedImage;
      
    • 提醒数字
      @property(nonatomic,copy) NSString *badgeValue;
      

代码

    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 设置窗口的跟控制器
    UITabBarController *tabBarVc = [[UITabBarController alloc] init];

    self.window.rootViewController = tabBarVc;

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

    vc.view.backgroundColor = [UIColor redColor];

    // 添加子控制器
    [tabBarVc addChildViewController:vc];

    //  设置按钮上面的内容
    vc.tabBarItem.title = @"消息";
    vc.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"];
    vc.tabBarItem.badgeValue = @"1000";

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

    vc1.view.backgroundColor = [UIColor greenColor];

    // 添加子控制器
    [tabBarVc addChildViewController:vc1];

    vc1.tabBarItem.title = @"联系人";
    vc1.tabBarItem.badgeValue = @"10";
    // 显示窗口
    [self.window makeKeyAndVisible];