UIScrollView 上具有垂直间距的动态 UIButton
我需要在 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
上,因此如果有更多 UIScrollView
的 contentSize
也应该增长strong>dataItems
,因此如果有超过 30 个 dataItems
,按钮可以滚动到屏幕外。
I need to space UIButtons out on a UIScrollView. The code I have works, however depending on the amount of dataItems
I have, the spacing is not even.
Snippet in Question
CGRectMake(10, ((120 / (count + 1)) * (i + 1) * 3) ,300,50)
Specifically
((120 / (count + 1)) * (i + 1) * 3)
Working code
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];
}
Screenshot
The example on the right should look like the example on the left as far as spacing goes. The UIButtons
are sitting on a UIScrollView
, so the UIScrollView
's contentSize
should also grow if there are more dataItems
so the buttons can scroll offscreen if there are say, 30+ dataItems
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你需要一个设定的大小和填充......
但是如果你试图让按钮来执行这种行为。我倾向于同意其余的。您可以制作一个带有按钮的自定义单元格。
然后,当表格需要按钮时,您可以加载该按钮单元格。
并且您的表与您的 [dataItems count] 相关联,以获取项目总数。
那么您唯一的计算就是在最初设置 dataItems 计数时设置单元格高度。确保不要让它们太短。桌子将处理剩下的事情。
sounds like you need a set size and padding...
however if you are trying to get buttons to do this sort of behavior. I am inclined to agree with the rest.. You can make a custom cell with a button in it.
Then you can load that button cell when the table asks for a button.
And you have the table tie to your [dataItems count] for the total of the items.
then your only calculation is to set the cell height when you initially set the dataItems count. making sure not to make them too short. and the table will handle the rest.
我同意古尔帕塔普的观点,但如果你执意要这么做,我会采取不同的做法。即使您现在了解 setFrame 语句中的数学原理,您或其他人稍后也会理解它。我肯定不会这么快看。为什么不做类似的事情:
I agree with Gurpartap, but if your dead set on this, I'd do it a bit differently. Even if you understand the math in that setFrame statement now, will you or someone else understand it later. I sure don't looking at it quick. Why not do something like:
你真的应该使用 UITableView 来代替。它自行完成滚动视图、单元格布局等。您只需指定单元格内容。
You should really use a UITableView instead. It does the scroll view, cell laying out, etc. all by itself. You just specify the cell contents.