向UIScrollview添加按钮

时间:2012-08-08 17:47:53

标签: iphone button uiscrollview

我希望它们一次显示在屏幕4上,所以我正在考虑为其创建一个UIScrollview并添加一个带有按钮的子视图。我怎么能添加让我们说20个按钮,因为我不能在屏幕上全部显示它们?

1 个答案:

答案 0 :(得分:0)

如何在数组中添加按钮。 这样的事情:(只是一个片段)

int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {

    UIImage *thumb = [_thumbs objectAtIndex:i];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(column*60+10, row*60+10, 60, 75);
    [button setImage:thumb forState:UIControlStateNormal];
    [button addTarget:self 
               action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside];
    button.tag = i; 

    [scrollView addSubview:button];

// If you want to show only 4 buttons, just tweak it here. You can do this method in anyway you like

    if (column == 4) {
        column = 0;
        row++;
    } else {
        column++;
    }

}
[scrollView setContentSize:CGSizeMake(300, (row+1) * 60 + 10)];

希望这有帮助。