代码改变世界

UITabbarController

2016-03-30 16:06  000aaa  阅读(158)  评论(0编辑  收藏  举报
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //1.创建Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //a.初始化一个tabBar控制器
    UITabBarController *tb = [[UITabBarController alloc]init];
    //设置控制器为Window的根控制器
    self.window.rootViewController = tb;
    /***生成下边的工具栏叫  UITabBar  高度为49 (不变)***/
    
    //b.创建子控制器------- 通过storyboard 创建的子控制器
    UIStoryboard *storyb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    FirstItemViewController *c1 = [storyb instantiateViewControllerWithIdentifier:@"FirstItemViewController"];
      [self setAttrbutesWith:c1 title:@"消息" iConName:@"" backgroundColor:[UIColor lightGrayColor]];
    
    UIViewController *c2 = [[UIViewController alloc]init];
    [self setAttrbutesWith:c2 title:@"联系人" iConName:@"" backgroundColor:[UIColor brownColor]];
    
    UIViewController *c3 =[ [UIViewController alloc]init];
    [self setAttrbutesWith:c3 title:@"动态" iConName:@"" backgroundColor:[UIColor cyanColor]];
    
    UIViewController *c4 = [[UIViewController alloc]init];
    c4.view.backgroundColor = [UIColor grayColor];//背景颜色
    c4.tabBarItem.title = @"设置";//tabBarItem名称
    c4.tabBarItem.image = [UIImage imageNamed:@"Defualt Icon name"];//TabBarButton默认图标
    c4.tabBarItem.selectedImage = [UIImage imageNamed:@"selected Icon Name"];//TabBarButton的选中图标
    c4.tabBarItem.badgeValue = @"999+";//TabBarButton右上角的提示数字
    /****  这些默认图标、选中图标的尺寸 30 x 30  ****/
   
    
    //c.将创建的子控制器添加到TabBarController中
    /*方式一  展示的顺序和添加的顺序一致*/
    [tb addChildViewController:c1];
    [tb addChildViewController:c2];
    [tb addChildViewController:c3];
    [tb addChildViewController:c4];
    
    /*方式二  展示的顺序和添加的顺序一致*/
    //tb.viewControllers = @[c1,c2,c3,c4];
    
    
    //d. 设置UITabbar的默认选中项/*此属性在 加入了子子控制器后才能生效,并且不能下标越界*/
    tb.selectedIndex = 2;
   
   
    //e.设置tabBar的背景颜色
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 49)];
    backView.backgroundColor = [UIColor clearColor];
    backView.alpha = 0.5f;
    [tb.tabBar insertSubview:backView atIndex:0];
    tb.tabBar.opaque = YES;
    
    //2. 设置Window 为主窗口 并显示出来
    [self.window makeKeyAndVisible];
    
    return YES;
}
- (void)setAttrbutesWith:(UIViewController *)vc title:(NSString *)title iConName:(NSString *)imageName backgroundColor:(UIColor *)color
{
    vc.view.backgroundColor = color;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
}