如何在滚动视图中添加多个按钮

4

我希望能够在滚动视图上动态添加按钮。假设有100个按钮需要添加到滚动视图上,那么将它们全部添加到nib文件中会非常繁琐。因此,我想知道如何编写代码,在滚动视图的图像视图上动态添加按钮。

2个回答

7
您需要做的是创建一个循环,创建 UIButtons。设置按钮并将它们作为子视图添加到 UIScrollView 中。代码如下所示。
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=100;
int buttonHeight=50;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame     = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
    [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:aButton];

    yCoord += buttonHeight + buffer;
}
[scrollView setContentSize:CGSizeMake(700, yCoord)];

我在做的事情是为X和Y坐标创建变量。当我循环创建UIButtons时,我会创建适当的CGRect结构来决定在UIScrollView中放置按钮的位置。在将该按钮添加到scrollView之后,更改X和Y值以放置下一个按钮。最后别忘了设置scrollview的ContentSize以启用滚动。
PS:所有这些代码都是手写的,可能有小的语法错误,但逻辑是正确的。

嗨,顺便说一下,它完美地工作了。buttonwidth和buttonheight是以像素为单位的,对吧? - raptor85
实际上你不需要担心这个问题。iOS会自动将像素规范化为X、Y坐标。因此,你可以在iPad或iPhone上运行相同的代码而不会出现问题。 - Srikar Appalaraju
谢谢,我还有一个问题。由于我创建了一些按钮,我需要在代码中添加位于XML URL上方的图像。 - raptor85
这是问题链接:http://stackoverflow.com/questions/8036152/adding-images-on-top-of-the-button-which-is-on-a-scrollview - raptor85

0
你可以使用以下代码在滚动视图上添加多个按钮:
步骤1:
Delegate "UIScrollViewDelegate" to your Viewcontroller.h

for example:
 @interface Viewcontroller : UIViewController<UIScrollViewDelegate>

步骤2:

//create you UIScrollView
UIScrollView  *Scroll_View= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,self.view.frame.size.width ,self.view.frame.size.height-0)];
Scroll_View.delegate= self;
self.automaticallyAdjustsScrollViewInsets= NO;
Scroll_View.backgroundColor= [UIColor clearColor];
Scroll_View.scrollEnabled= YES;
Scroll_View.userInteractionEnabled= YES;
[Scroll_View setShowsHorizontalScrollIndicator:NO];
[Scroll_View setShowsVerticalScrollIndicator:YES];
[self.view addSubview:Scroll_View];

步骤三:

NSArray  *optionsAry= [NSArray arrayWithObjects:@"My Profile & My Orders" ,@"Track Order" ,@"Settings" ,@"Contact Us" ,@"About Us" ,@"Terms & Conditions" ,@"Currency Converter" ,@"System Info" ,@"e-Commerce News App (News Billa)" ,@"Social Media" ,nil];

int y= 20;
for(int i=0; i<optionsAry.count; i++){
    UIButton  *BTN_For_More= [[UIButton alloc] initWithFrame:CGRectMake(20 ,y , Scroll_View.frame.size.width-40 ,35)];
    BTN_For_More.tag= i;
    BTN_For_More.backgroundColor= [UIColor whiteColor];
    [BTN_For_More addTarget:self action:@selector(BTN_For_More_Action:) forControlEvents:UIControlEventTouchUpInside];
    BTN_For_More.titleLabel.text= [NSString stringWithFormat:@"%@",[optionsAry objectAtIndex:i]];
    BTN_For_More.titleLabel.textColor= [UIColor colorWithRed:12/255.0 green:104/255.0 blue:172/255.0 alpha:1.0f];
    BTN_For_More.titleLabel.textAlignment= NSTextAlignmentCenter;
    BTN_For_More.titleLabel.font= [UIFont fontWithName:@"SourceSansPro-Regular" size:15];
    //3D effects Start
    BTN_For_More.layer.shadowColor= [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5f].CGColor;
    BTN_For_More.layer.shadowOffset= CGSizeMake(0 ,0);//2,2
    BTN_For_More.layer.shadowRadius= 2.0;
    BTN_For_More.layer.shadowOpacity= 1.0;
    BTN_For_More.layer.masksToBounds= NO;
    //3D effects End


    [Scroll_View addSubview:BTN_For_More];

    y= BTN_For_More.frame.size.height + y + 20;
}//for loop END //10

Scroll_View.contentSize= CGSizeMake(Scroll_View.frame.size.width ,y);

步骤4:

-(void) BTN_For_More_Action:(NSString *)myString{
   NSLog(@"you clicked on button %@", myString);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接