【发布时间】:2011-05-11 00:15:09
【问题描述】:
我需要在 UIScrollView 上分隔 UIButtons。我的代码有效,但是根据我拥有的dataItems 的数量,间距不均匀。
问题中的片段
CGRectMake(10, ((120 / (count + 1)) * (i + 1) * 3) ,300,50)
具体
((120 / (count + 1)) * (i + 1) * 3)
工作代码
int count = [dataItems count]; /* not a specific value, can grow */
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton setFrame: CGRectMake(10,((120 / (count + 1)) * (i + 1) * 3) ,300,50) ];
[aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:aButton];
}
截图
就间距而言,右侧的示例应与左侧的示例相似。 UIButtons 位于 UIScrollView 上,因此如果有更多 dataItems,UIScrollView 的 contentSize 也应该会增长,因此如果有说,按钮可以滚动到屏幕外, 30+dataItems.
【问题讨论】:
标签: iphone ios uiscrollview for-loop spacing