1

我遇到了我认为有点像新手的问题......我制作了一个动态滚动视图,其中图像和标签以编程方式添加为子视图。问题是只显示我添加为子视图的最后一个。此外,当我读到“addSubview:”时,它会说“将视图添加到接收者的子视图列表的末尾”。这是否意味着只显示最后添加的子视图?在那种情况下,我如何使两者都可见?

在此先感谢,汤姆

代码:

for(int i = 0; i < [famorableArray count]; i++){

    UIButton *famorableButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [famorableButton setFrame:CGRectMake(0.0f, 5.0f, 57.0f, 57.0f)];
    [famorableButton setImage:personLogo forState:UIControlStateNormal]; 

    NSString *famString = [NSString stringWithFormat:@"%@", [[[famorableArray objectAtIndex:i] substringFromIndex:8] capitalizedString]];
    NSLog(@"%@", famString);
    UILabel *famLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    famLabel.text = famString;
    NSLog(@"Test2 %@", famLabel.text);

    // Move the buttons position in the x-demension (horizontal).
    CGRect btnRect = famorableButton.frame;
    btnRect.origin.x = totalButtonWidth;
    [famorableButton setFrame:btnRect];

    CGRect labelRect = famLabel.frame;
    labelRect.origin.x = totalButtonWidth + 28.5f;
    [famLabel setFrame:btnRect];


    // Add the button to the scrollview
    [famScroll addSubview:famLabel];
    [famScroll addSubview:famorableButton];

    // Add the width of the button to the total width.
    totalButtonWidth += famorableButton.frame.size.width + 30;

}
[famScroll setContentSize:CGSizeMake(totalButtonWidth, 79.0f)];
4

2 回答 2

1

对于每个添加的子视图,您必须设置frame属性。框架是视图在父视图中的位置。“添加到子视图列表”并不意味着任何自动布局。所以我假设你所有的子视图都是可见的但重叠。

于 2012-07-28T16:13:29.900 回答
0

我的错...复制粘贴后编辑失败。不小心将 btnRect 用作​​标签的框架。无论如何感谢您的努力@Martin :)

于 2012-08-04T13:24:41.980 回答