iOS - 一个方法搞定自定义tabBarItem中的控件位置均分

京东 qq 自定义tabbar控件均分

这里写图片描述
这里写图片描述

  • 代码如下
//ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    [self setLablesWithArrOfTitle:@[@"one",@"哈哈",@"你妹的",@"我去"] andLeftDistance:15 andItHeight:21 andYcoordinate:255 andDestiView:self.view];
    [self setLablesWithArrOfTitle:@[@"one",@"哈哈",@"你妹的",@"我去"] andLeftDistance:15 andItWidth:44 andItHeight:21 andYcoordinate:155 andDestiView:self.view];
}

对应方法

  • 为了对比明显将控制器背景色,以及控件背景色都设置上了不同的颜色以便于观看
自动计算label的宽度
/**
 *  设置中间部分控件两两之间距离相等,两端控件距离两侧为中间控件的一半
 *  根据lDis计算label宽度
 *  @param arrTitles label的名字数组
 *  @param lDistance 最左边控件距离屏幕左边的距离
 *  @param height    控件高度
 *  @param y         控件y坐标
 */
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{

    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = 2 * lDistance;
    CGFloat width = (screenWidth - arrTitles.count * 2 * lDistance)/arrTitles.count;
    UILabel * tempLabel = [UILabel new];

    for (int i = 0; i< arrTitles.count; i++) {

        if (i == 0) {

            UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
            lbl.backgroundColor = [UIColor whiteColor];
            lbl.textAlignment = NSTextAlignmentCenter;
            lbl.text = arrTitles[0];
            [destiView addSubview:lbl];
            tempLabel = lbl;
            continue;
        }
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.backgroundColor = [UIColor whiteColor];
        lbl.text = arrTitles[i];
        [destiView addSubview:lbl];
        tempLabel = lbl;
    }

}
自定义label的宽度
/**
 *  设置中间部分控件两两之间距离相等
 *  自定义label宽度
 *  @param arrTitles label的名字数组
 *  @param lDistance 最左边控件距离屏幕左边的距离
 *  @param width     控件宽度
 *  @param height    控件高度
 *  @param y         控件y坐标
 */
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItWidth:(CGFloat)width andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{

    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = (screenWidth - 2*lDistance - width*arrTitles.count) / (arrTitles.count - 1);
    UILabel * tempLabel = [UILabel new];

    for (int i = 0; i< arrTitles.count; i++) {

        if (i == 0) {

            UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
            lbl.backgroundColor = [UIColor whiteColor];
            lbl.textAlignment = NSTextAlignmentCenter;
            lbl.text = arrTitles[0];
            [destiView addSubview:lbl];
            tempLabel = lbl;
            continue;
        }
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
        lbl.backgroundColor = [UIColor whiteColor];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = arrTitles[i];
        [destiView addSubview:lbl];
        tempLabel = lbl;
    }

}

最终效果

  • 第一行是自定义控件宽度
  • 第二行是自动计算控件宽度 根据自己需求调用即可
  • 方法内部稍加修改就可以作为创建UIButton,UIImageView,UITxtField等等.
    效果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值