for (int i=0; i<[self.imagearray count]; i++) {
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake(i%3*(SCREEN_WIDTH/3-1), i/3*(SCREEN_WIDTH/3-1)+self.view2.frame.origin.y+self.view2.frame.size.height+5,SCREEN_WIDTH/3-1,SCREEN_WIDTH/3-1);
self.Statebutton=button1;
NSString *str = [_imagearray objectAtIndex:i];
button.tag =i;
[button setBackgroundImage:[UIImage imageNamed:[[IhuikouPublicTools getImagePath]stringByAppendingPathComponent:str]]forState:UIControlStateNormal];
[button addTarget:self action:@selector(change:) forControlEvents: UIControlEventTouchUpInside ];
[button setImage:nil forState:UIControlStateNormal];
[self.scrollview addSubview:button];
}
原文地址:http://blog.csdn.net/yangchen9931/article/details/45039679