2012-01-06 46 views
-1

我是iPhone新手,我想知道如何在UIScrollView中添加UIButton在UIScrollView中添加UIButtons编程

这些按钮应该有所不同12202428

有没有从我们可以动态地做到这一点的任何代码,而不是从笔尖文件,有没有样品avaliable?

在此先感谢。

回答

4

当然,你可以用一些手动添加按钮,如下所示:

[scrollView addSubview:yourButton]; 

您可以通过IB创建两件事,并设置按钮的框架通过代码

0

,你可以这样做:

[self.scrollView setScrollEnabled:YES]; 
[self.scrollView setFrame:CGRectMake(0, 70,320, 70)]; 
[self.scrollView setContentSize:CGSizeMake(2370, 70)]; 

int x = 0; 
for (int i=0; i<[your array count]; i++) { 

    // view allocation 
    ButnView=[[UIView alloc] init]; 
    [ButnView setFrame:CGRectMake(x, 0, 82, 70)]; 

    // label allocation 
    UILabel* butnheaderlabel = [[UILabel alloc] initWithFrame:CGRectMake(14, -10, 80, 70)]; 
    UILabel* butnfooterlabel = [[UILabel alloc] initWithFrame:CGRectMake(27, 10, 80,70)]; 
    [butnheaderlabel setFont:[UIFont systemFontOfSize:14.0]]; 

    // button allocation 
    btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn setFrame:CGRectMake(0, 0,82, 70)]; 
    [btn setBackgroundColor:[UIColor clearColor]]; 
    [btn setTag:i]; 
    [[btn layer] setBorderWidth:1.0f]; 
    [[btn layer] setBorderColor:[UIColor grayColor].CGColor]; 
    NSString*resourceKey=[your array objectAtIndex:i]; 
    NSArray*seperatedStr=[resourceKey componentsSeparatedByString:@","]; 
    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [btn addSubview:butnheaderlabel]; 
    [btn addSubview:butnfooterlabel]; 
    [ButnView addSubview:btn]; 
    [self.scrollView addSubview:ButnView]; 
    x+=81; 
} 
相关问题