自动布局(Masonry)设置tabbar

 

 

 

//自定义标签工具栏
- (void) initTabBarView{
//    
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight-tabViewHeight-1,kScreenWidth, tabViewHeight+1)];
    [self.view addSubview:self.bottomView];
    
    UIView  *upLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth, 0.5)];
    upLine.backgroundColor =  RGB(0xd4, 0xdd, 0xe3);
    upLine.alpha = 0.16;
    [self.bottomView addSubview:upLine];
    
   UIView  *downLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0.5 ,kScreenWidth, 0.5)];
   downLine.backgroundColor =  RGB(0x34, 0x53, 0x6a);
   downLine.alpha = 0.16;
   [self.bottomView addSubview:downLine];
    
    _tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, kScreenWidth, tabViewHeight)];
    _tabBarView.backgroundColor = [UIColor whiteColor];
    
    [self.bottomView addSubview:_tabBarView];
    
    NSArray *textArr = @[@"通讯录",@"常用联系人",@"个人中心"];
    NSArray *imageNameArray = @[@"tab_ico_contact_nor.png",@"tab_ico_contact_hl.png",
                                @"tab_ico_collect_nor.png",@"tab_ico_collect_hl.png",
                                @"tab_ico_me_nor.png",@"tab_ico_me_hl.png"];
    int tabCount =(int)textArr.count;
    
    NSMutableArray *spaceViews = [NSMutableArray arrayWithCapacity:tabCount+1];
    
    for (int i=0; i < tabCount+1; i++) {
        UIView  *v = [UIView new];
        [spaceViews addObject:v];
        [self.tabBarView addSubview:v];
        
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(tabViewHeight);
            make.centerY.mas_equalTo(self.tabBarView.mas_centerY);
        }];
    }
    [spaceViews[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.tabBarView.mas_left);
        
    }];
    for(int i= 0;i< textArr.count;i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100+i;
        [btn setImage:[UIImage imageNamedWithoutCache:imageNameArray[i*2] ] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamedWithoutCache:imageNameArray[i*2+1] ] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamedWithoutCache:imageNameArray[i*2+1] ] forState:UIControlStateHighlighted];
        [btn setTitle:textArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(0x7c, 0x86, 0x8d) forState:UIControlStateNormal];
        [btn setTitleColor:RGB(0, 0x9c, 0xff) forState:UIControlStateHighlighted];
        [btn setTitleColor:RGB(0, 0x9c, 0xff) forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize: 18/2];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(kScreenWidth/tabCount*i + kScreenWidth/(tabCount*4), (tabViewHeight-btnHeight)/2,kScreenWidth/(2*tabCount) , btnHeight); //kScreenWidth/(2*tabCount)  btn.bounds.size.width
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.image.size.height, -btn.imageView.image.size.width, 0, 0)];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0,btn.titleLabel.bounds.size.height, -btn.titleLabel.bounds.size.width)];

       [self.tabBarView addSubview:btn];
        
        [spaceViews[i+1] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(btn.mas_right);
            make.width.equalTo(((UIView *)spaceViews[i]).mas_width);
        }];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tabBarView.mas_top).offset((tabViewHeight-btnHeight)/2);
            make.bottom.equalTo(self.tabBarView.mas_bottom).offset(-(tabViewHeight-btnHeight)/2);
            make.height.equalTo(@33);
            make.left.equalTo(((UIView *)spaceViews[i]).mas_right);
            make.right.equalTo(((UIView *)spaceViews[i+1]).mas_left);
        }];
        if(i == 0)
        {
            [btn setSelected:YES];
        }
        
    }
    
    [spaceViews[tabCount] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.tabBarView.mas_right);
    }];

}

 

posted @ 2015-07-13 11:24  尘恍若梦  阅读(841)  评论(0编辑  收藏  举报