[英]Add UIButtons in UIScrollView Programmatically
I am new to iPhone development, and I want to know how can I add UIButton
s in a UIScrollView
? 我是iPhone开发的新手,我想知道如何在UIScrollView
添加UIButton
?
These buttons should be vary from 12
to 20
, 24
, 28
etc. 这些按钮应该变化从12
到20
, 24
, 28
等。
Is there any code from that we can do this dynamically, not from Nib file, and is there any samples avaliable? 有没有可以动态执行此操作的代码,而不是来自Nib文件的代码,并且有可用的示例吗?
Thanks in advance. 提前致谢。
Sure, you can add the buttons manually with something like the following: 当然,您可以手动添加按钮,如下所示:
[scrollView addSubview:yourButton];
You can create both things via IB and set the button's frame vía code 您可以通过IB创建两者,并设置按钮的框架代码
you can do like this: 您可以这样:
[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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.