UIScrollView 上具有垂直间距的动态 UIButton

发布于 2024-11-06 00:15:57 字数 1188 浏览 6 评论 0原文

我需要在 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 上,因此如果有更多 UIScrollViewcontentSize 也应该增长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.

         enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2024-11-13 00:15:57

听起来你需要一个设定的大小和填充......

int count = [dataItems count];
CGFloat staticX = 10; // Static X for all buttons.
CGFloat staticWidth = 300; // Static Width for all Buttons.
CGFloat staticHeight = 50; // Static Height for all buttons.

CGFloat staticPadding = 10; // Padding to add between each button.

for (int i = 0; i < count; i++) {
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton setTag:i];
    //Calculate with the Static values.
    //I added one set of padding for the top. then multiplied padding plus height for the count.
    // 10 + 0 for the first
    // 10 + 60 for the second.
    // 10 + 120 for the third. and so on.
    [aButton setFrame: CGRectMake(10,(staticPadding + (i * (staticHeight + staticPadding)) ,staticWidth,staticHeight) ];
    [aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
    [scroller addSubview:aButton];
}

但是如果你试图让按钮来执行这种行为。我倾向于同意其余的。您可以制作一个带有按钮的自定义单元格。

然后,当表格需要按钮时,您可以加载该按钮单元格。

并且您的表与您的 [dataItems count] 相关联,以获取项目总数。

那么您唯一的计算就是在最初设置 dataItems 计数时设置单元格高度。确保不要让它们太短。桌子将处理剩下的事情。

sounds like you need a set size and padding...

int count = [dataItems count];
CGFloat staticX = 10; // Static X for all buttons.
CGFloat staticWidth = 300; // Static Width for all Buttons.
CGFloat staticHeight = 50; // Static Height for all buttons.

CGFloat staticPadding = 10; // Padding to add between each button.

for (int i = 0; i < count; i++) {
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton setTag:i];
    //Calculate with the Static values.
    //I added one set of padding for the top. then multiplied padding plus height for the count.
    // 10 + 0 for the first
    // 10 + 60 for the second.
    // 10 + 120 for the third. and so on.
    [aButton setFrame: CGRectMake(10,(staticPadding + (i * (staticHeight + staticPadding)) ,staticWidth,staticHeight) ];
    [aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
    [scroller addSubview:aButton];
}

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.

白云不回头 2024-11-13 00:15:57

我同意古尔帕塔普的观点,但如果你执意要这么做,我会采取不同的做法。即使您现在了解 setFrame 语句中的数学原理,您或其他人稍后也会理解它。我肯定不会这么快看。为什么不做类似的事情:

CGRect frm = CGRectMake(10,10,300,50);
for (int i = 0; i < count; i++) {
  UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  [aButton setFrame: frm];
  frm.origin.y = frm.origin.y + aButton.frame.size.height + 10;
  etc, etc

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:

CGRect frm = CGRectMake(10,10,300,50);
for (int i = 0; i < count; i++) {
  UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  [aButton setFrame: frm];
  frm.origin.y = frm.origin.y + aButton.frame.size.height + 10;
  etc, etc
花开半夏魅人心 2024-11-13 00:15:57

你真的应该使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文