2012-08-08 168 views
0

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

回答

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

希望这有助于。

+0

你好,感谢您的回答我是一个新手,我的问题是我如何添加按钮数组? – XpApp 2012-08-08 18:07:59

+0

只是在你的头文件中实例化它并在你的m文件中实现它。我已经使用这个代码,所以它的工作。 – Bazinga 2012-08-08 18:11:17