UIScrollView * buttonView = [[UIScrollView alloc]init];
buttonView.showsHorizontalScrollIndicator = NO;
buttonView.showsVerticalScrollIndicator = NO;
buttonView.backgroundColor= [UIColorgreenColor];
[whightViewaddSubview:buttonView];
CGFloatpointX =16.0;
CGFloatpointY =5;
CGFloatallWidth =self.frame.size.width-20;
UIFont*titleFont = [UIFontsystemFontOfSize:15];
for(inti =0; i <12; i++) {
// CGRect rect = [self.CardArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) self.CardArray.count options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont} context:nil];
CGFloat width =140;//rect.size.width + 30;
if(pointX + width > allWidth) {//换行
pointX =16;//X从新开始
pointY +=40;//换行后Y+
}
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
but.frame=CGRectMake(pointX, pointY, width,32);
but.tag= i +1000;
[butaddTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
but.layer.masksToBounds = YES;
but.layer.cornerRadius=10;
but.backgroundColor = [UIColor redColor];
[butsetTitleColor:[UIColor blackColor ] forState:UIControlStateNormal];
// [but setTitle:self.CardArray[i] forState:UIControlStateNormal];
but.titleLabel.font= titleFont;//一定要一样
pointX += (width +5);//每次X都加上button宽和间距5
[buttonViewaddSubview:but];
}
if(cardArray.count<10) {
buttonView.frame=CGRectMake(0, titleLabel.Y+titleLabel.height+10,self.width,140);
buttonView.contentSize=CGSizeMake(self.width, pointY +35);
}else{
buttonView.frame=CGRectMake(0, titleLabel.Y+titleLabel.height+10,self.width,180);
buttonView.contentSize=CGSizeMake(self.width, pointY +35);
}