0

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

4

1 回答 1

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

希望这可以帮助。

于 2012-08-08T17:52:53.610 回答