gpt4 book ai didi

objective-c - 如何显示多个 subview ?

转载 作者:行者123 更新时间:2023-11-29 13:31:40 24 4
gpt4 key购买 nike

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

提前致谢,汤姆

代码:

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)];

最佳答案

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

关于objective-c - 如何显示多个 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com