自动布局(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);
    }];

}

 

转载于:https://www.cnblogs.com/developer-qin/p/4642402.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值