2012-01-06 71 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; 
} 
相關問題