码迷,mamicode.com
首页 > 其他好文 > 详细

tabbar的自定义

时间:2016-04-22 18:15:37      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

[self createCustomTabBar];

-(void)createCustomTabBar{
    //创建一个UIImageView,作为底图
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-49, 320, 49)];
    bgView.image = [UIImage imageNamed:@"tabbg.png"];
    bgView.tag = 999;
    //开启imageview的交互属性
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
   
    
    //创建button实例  模拟tabBarItem
    //每个button的间
    NSArray *array=[NSArray arrayWithObjects:@"IconSettings",@"IconProfile",@"IconHome",@"IconEmpty",@"IconCalendar",@"btn_bottom1_on.png",@"btn_bottom2_on.png",@"btn_bottom3_on.png",@"btn_bottom4_on.png",@"btn_bottom5_on.png", nil];
    for (int i = 0; i<(array.count)/2; i++) {
        NSString *imageName = [array objectAtIndex:i];
        NSString *selectedImage = [array objectAtIndex:i+5];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0+64*i, 0, 64, 49)];
        //设置button处于常规状态下的背景图片
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        //设置button处于选中状态下得背景图片
        [btn setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        [btn addTarget:self
                action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        
        if (i==0) {
            btn.selected = YES;
        }
        
        [bgView addSubview:btn];
    }
}
-(void)btnClicked:(UIButton *)btn
{
    //点击不同的按钮,切换不同的视图控制器
    self.selectedIndex = btn.tag;
    //切换不同btn的显示状态
    UIImageView *bgView = (UIImageView *)[self.view viewWithTag:999];
    for (UIView *subView in bgView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *btn1 = (UIButton *)subView;
            if (btn1.tag == btn.tag) {
                btn1.selected = YES;
            }else{
                btn1.selected = NO;
            }
        }
    }
}

tabbar的自定义

标签:

原文地址:http://www.cnblogs.com/linxiu-0925/p/5422113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!