CGFloat height = self.scrollView.bounds.size.height
CGFloat btnX = 5
for (int index = 0
UIButton *button = [[UIButton alloc] init]
[self.scrollView addSubview:button]
[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected]
button.titleLabel.font = [UIFont systemFontOfSize:16]
NSString *title = dataSource[index]
[button setTitle:title forState:UIControlStateNormal]
//计算宽
CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:button.titleLabel.font.fontName size:button.titleLabel.font.pointSize]}]
CGFloat width = titleSize.width+10
button.frame = CGRectMake(btnX, 0, width, height)
[button addTarget:self action:@selector(categoriesBtnAction:) forControlEvents:UIControlEventTouchUpInside]
btnX += button.frame.size.width
}
self.scrollView.contentSize = CGSizeMake(btnX, height)