【问题标题】:Dynamic UIButtons with vertical spacing on a UIScrollViewUIScrollView 上具有垂直间距的动态 UIButton
【发布时间】: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 上,因此如果有更多 dataItemsUIScrollViewcontentSize 也应该会增长,因此如果有说,按钮可以滚动到屏幕外, 30+dataItems.

      

【问题讨论】:

    标签: iphone ios uiscrollview for-loop spacing


    【解决方案1】:

    听起来您需要设置大小和填充...

    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 计数时设置单元格高度。确保不要让它们太短。其余的由桌子处理。

    【讨论】:

      【解决方案2】:

      我同意 Gurpartap 的观点,但如果您对此深信不疑,我会采取不同的做法。即使您现在理解了该 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
      

      【讨论】:

        【解决方案3】:

        您应该真正使用 UITableView。它自己完成滚动视图、单元格布局等。您只需指定单元格内容。

        【讨论】:

        • 对使用 UITableView 不感兴趣;按钮将具有特定的背景图像,这比子类化表格要容易得多。
        • 子类化 UITableViewCell 并不那么重要。只需做 cell.backgroundView = ...;但是,只要您已经完成了该部分的开发,就可以了。
        • @Jason 的方法完全符合我的预期。子类化 UITableView 是一种比公认答案更密集的代码方法。
        猜你喜欢
        • 2021-05-22
        • 2013-09-03
        • 2015-06-11
        • 2015-11-10
        • 1970-01-01
        • 2012-06-15
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多