【问题标题】:Add Multiple UIButton to a ScrollView将多个 UIButton 添加到 ScrollView
【发布时间】:2015-09-22 10:01:13
【问题描述】:

伙计们,我正在尝试在滚动视图中动态添加多个按钮

到目前为止,我有这个代码

for (int i = 0; i < numberOfButtonInMenu; i++)
{
    UIButton *aButton = [UIButton new];
    UILabel *tButton = [UILabel new];
    UILabel *sButton = [UILabel new];
    UIImageView *iButton = [UIImageView new];

    tButton = title;
    sButton = subtitle;
    iButton = image;

    tButton.hidden = NO;
    sButton.hidden = NO;
    iButton.hidden = NO;
    [tButton setText:Titles[i]];
    [sButton setText:Subtitles[i]];        
    [tButton sizeToFit];
    [sButton sizeToFit];

    iButton.image = [UIImage imageNamed:Images[i]];
    aButton.frame = CGRectMake(xCoord, yCoord, screenWidth, buttonHeight);
    [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
    [aButton addSubview:tButton];
    [aButton addSubview:sButton];
    [aButton addSubview:iButton];
    [multiMenuScroll addSubview:aButton];
    yCoord += buttonHeight + buffer;
}

}

我把我所有的设计都设置在这个for之前。 当我显示我的multiMenuScrollView 时,我最终只显示了最后一个按钮。 (所以对于numberOfButtonInMenu = 3;,我只会显示第三个按钮)

看起来像这样:http://i.stack.imgur.com/5UU6S.jpg

在“Explore”和“Around You”之间还有另外两个 Button,但里面什么都没有,这有点奇怪。 你们对我做错了什么有任何想法吗?

【问题讨论】:

  • 所以对于 numberOfButtonInMenu = 3 你有第三个按钮显示。你什么意思?
  • 只显示最后一个按钮,在正确的位置。其实另外2个也在,我可以点一下,但是都是空的。
  • 你设置好multiMenuScroll的contentSize了吗?
  • abyt07 :是的,一切正常,但我只能看到最后一个按钮(其他按钮在这里,但不可见/为空)
  • @iphonic :我在for 之前这样做。 labelsubtitleimage,已经有了好的框架(大小和原点),编辑:我试过了,而且......好吧,和以前一样的问题,只显示最后一个按钮:/跨度>

标签: ios objective-c uibutton scrollview


【解决方案1】:

因为您将 aButton1 中的 tButton sButton iButton 再次添加到 aButton3。 show there' 只会在最后一个视图中显示(aButton3)。如果想像您的代码一样将tButton sButton iButton 添加到每个按钮,您需要在每个for 循环中初始化新的tButton sButton iButton

错误代码:

tButton = title;
sButton = subtitle;
iButton = image;

它必须是这样的:

UILabel *tButton = [[UILabel alloc] initWithFrame:...];
UILabel *sButton = [[UILabel alloc] initWithFrame:...];
UIImageView *iButton = [[UIImageView alloc] initWithFrame:...];

【讨论】:

  • 那是我的第一次,这些 UILabel 和 UIImage 的实例是相同的。但后来我添加了第一行:UILabel *tButton = [UILabel new]; UILabel *sButton = [UILabel new]; UIImageView *iButton = [UIImageView new];,它仍然是一样的。 (我还在每行末尾添加init] 以确保它实际上是 init,
  • 您的初始化代码失败,因为在初始化之后您为 tButton sButton iButton 分配了标题字幕图像。并注意'title subtitle image'没有分配增益,仍然有旧对象
  • 你需要删除:tButton = title; sButton = 字幕; iButton = 图像;
  • @Abhinav 他确实更改了 yCoord 以在每个循环中为按钮制作不同的框架
  • 噢噢耶,非常感谢!我这样做是因为我想在进入循环之前修复一些东西(比如准备 UILabel,使用好的字体,好的背景等......)但是我会在循环中为每个按钮设置它^^'谢谢再次交配!
【解决方案2】:

难怪……

您没有正确设置框架。在for 循环中,每次创建新按钮UIButton *aButton = [UIButton new]; 并将子视图添加到multiMenuScroll。所有这些都被添加但在同一个地方。所以,最后只出现了第三个按钮。

您需要正确设置按钮的框架。

UIButton *aButton = [[UIButton alloc] initWithFrame:<Set_Your_Frame>]; // Change X or Y value as per for loop iteration.

作为旁注,您将标签命名为按钮后缀,例如 tButtonsButton。以编程方式并没有错,但它们令人困惑!

【讨论】:

  • 是的',名字很混乱,我现在就改一下^^'谢谢你的回答:)
【解决方案3】:

vwScroll = 滚动视图的名称 tagsArray = NSStrings 数组。这些字符串将是标签的文本,它将以水平方式添加到滚动视图中

-(void)setScrollViewWithArray:(NSArray *)tagsArray{
    CGFloat xAxis = 0.0f;
    CGFloat gapBwLabels = 10.0f;
    [vwScroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i = 0; i<tagsArray.count; i++) {
        NSString *tagName = [tagsArray objectAtIndex:i];
        CGSize sizeOfTagString = [self getSizeOfText:tagName];
        CGFloat width = sizeOfTagString.width+gapBwLabels;

        CGRect tagLabelframe = CGRectMake(xAxis, 2, width, 30);
        UILabel *lblTag = [[UILabel alloc]initWithFrame:tagLabelframe];
        [self setLabel:lblTag withBackgroundColor:[UIColor whiteColor]];
        [lblTag setText:tagName];
        [vwScroll addSubview:lblTag];
        xAxis = xAxis + width + gapBwLabels;
    }

    [vwScroll setContentSize:CGSizeMake(xAxis, vwScroll.frame.size.height)];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多