根据button的tag与view同步 view根据button文字长短变化

 NSArray *typAry = @[@"四个字",@"22",@"33",@"44",@"44",@"44"];

    for (NSInteger i = 0; i < 6; i++) {

        UIButton *typBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/6.0*i+5, 64, ScreenWidth/6.0, 38)];

        typBtn.backgroundColor = [UIColor whiteColor];

        [typBtn setTitle:typAry[i] forState:UIControlStateNormal];

        [typBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

        typBtn.titleLabel.font = [UIFont systemFontOfSize:14];

        typBtn.tag = i;

       

        if (i == 0) {

            

            [typBtn setTitleColor:customColor(45, 166, 252, 1.0) forState:UIControlStateNormal];

            

        }

        else{

            [typBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        }

        [typeBtnAry addObject:typBtn];

        [_headView addSubview:typBtn];

//下划线

    UIButton * bbbtn  =typeBtnAry[0];

    CGSize sizeText =  [bbbtn.titleLabel.text boundingRectWithSize:CGSizeMake(200, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil] context:nil].size;

    slineView = [[UIView alloc]initWithFrame:CGRectMake(0, 98, sizeText.width, 2)];

    slineView.backgroundColor = customColor(45, 166, 252, 1.0);

    slineView.centerX = bbbtn.centerX;

    [_headView addSubview:slineView];

 

button点击事件里

 

if (typeIndex != btn.tag) {

        UIButton *lastBtn = typeBtnAry[typeIndex];

        [lastBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        CGSize sizeText =  [btn.titleLabel.text boundingRectWithSize:CGSizeMake(200, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil] context:nil].size;

        [btn setTitleColor:customColor(45, 166, 252, 1.0) forState:UIControlStateNormal];

        

        [ UIView animateWithDuration:0.25 animations:^{

            slineView.width =sizeText.width;

            slineView.centerX = btn.centerX;

            

        }];

 

        }];

 

转载于:https://www.cnblogs.com/xiaodeng90/p/7743460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值