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;
}];
}];