UITabBar UITabBarController

//UITabBar 标签栏,是标签栏控制器view的子视图 (320*49),UITabBarItem 展示在UITabBar上
    //nav1.tabBarItem.title = @"主页";
    //title属性,能够同时设置navigationItem.title和tabBarItem.title
    vc1.title = @"主页";
    //nav1.navigationItem.title
    //设置图片,图片最佳显示大小(30*30)视网膜屏:(60*60)
    nav1.tabBarItem.image = [UIImage imageNamed:@"tab_0.png"];
    
    //badgeValue 设置徽标
    vc4.tabBarItem.badgeValue = @"4";
    
    
    //标签栏控制器,特殊的vc,能够管理其他vc
    //UITabBarController *tab
    UITabBarController *tab = [[UITabBarController alloc] init];
    //接收带有vc对象的数组,当数组中vc对象超过5个时,标签栏控制器会自动生成一个moreNavigaitonController,原来第5、6个模块通过列表的形式呈现
    //tab.moreNavigationController
    tab.viewControllers = controllers;
    //设置选中的模块
    //tab.selectedIndex =1;
    //tab.selectedViewController = controllers[1];
    
    //设置代理
    tab.delegate = self;
//切换底部的视图控制器时,触发此方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSLog(@"vc title:%@",viewController.title);
    //当点击的时候储存标题
}


//完全自定义
- (void) createControllers{
    NSString * path = [[NSBundle mainBundle] pathForResource:@"LDYTabBarController" ofType:@"plist"];
    CGRect frame = [[UIScreen mainScreen] bounds];
    UIImageView * bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 49)];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.tag = 999;
    bgImageView.image = [UIImage imageNamed:@"tabbar_background"];
    NSArray * array = [NSArray arrayWithContentsOfFile:path];
    NSInteger space = (self.view.frame.size.width - kBtnWidth * array.count) / (array.count + 1);
    NSMutableArray * controllers = [NSMutableArray array];
    for (NSInteger i = 0 ; i < [array count]; i++) {
        NSDictionary * dict = [array objectAtIndex:i];
        NSString * className = [dict objectForKey:@"controller"];
        Class  class = NSClassFromString(className);
        UIViewController * vc = [[class alloc] init];
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [controllers addObject:nav];
        NSString * imageName = [dict objectForKey:@"iconName"];
        NSString * selectImageName = [imageName stringByAppendingString:@"_select"];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
        btn.tag = i + 800;
        btn.frame = CGRectMake(space + i * (space + kBtnWidth), 0, kBtnWidth, kBtnHeight);
        [btn addTarget:self action:@selector(btnClicked:)  forControlEvents:UIControlEventTouchUpInside];
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(space + i * (space + kBtnWidth) , 28, kBtnWidth, 19)];
        [label setFont:[UIFont systemFontOfSize:11]];
        label.textAlignment = NSTextAlignmentCenter;
        [label setText:[dict objectForKey:@"title"]];
        [label setTextColor:[UIColor blackColor]];
        label.tag = i + 900;
        [label setHighlightedTextColor:[UIColor orangeColor]];
        if (i == 0) {
            btn.selected = YES;
            label.highlighted = YES;
            _oldSelectedBtnTag = 800;
        }
        [bgImageView addSubview:btn];
        [bgImageView addSubview:label];
    }
    self.viewControllers = controllers;
    [self.tabBar addSubview:bgImageView];
    
}

- (void) btnClicked:(UIButton *)btn{
    UIButton * oldBtn = (UIButton *)[self.view viewWithTag:_oldSelectedBtnTag];
    oldBtn.selected = NO;
    UILabel * oldLabel = (UILabel *)[self.view viewWithTag:_oldSelectedBtnTag + 100];
    oldLabel.highlighted = NO;
    UILabel * label = (UILabel *)[self.view viewWithTag:btn.tag + 100];
    label.highlighted = YES;
    btn.selected = YES;
    _oldSelectedBtnTag = btn.tag;
    self.selectedIndex = btn.tag - 800;
    UIImageView * bgImageView = (UIImageView *)[self.view viewWithTag:999];
    [self.tabBar bringSubviewToFront:bgImageView];
    
}

 

posted @ 2015-04-07 20:15  孤独的根号下三  阅读(179)  评论(0编辑  收藏  举报