gpt4 book ai didi

iphone - 将 View 添加到 ScrollView 中

转载 作者:行者123 更新时间:2023-11-29 04:52:25 26 4
gpt4 key购买 nike

我创建了一个 ScrollView ,并在 ScrollView 上添加了两个备用自定义 UIView

@interface FirstView : UIView
@interface SecondView : UIView

我的数组是

 NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView,  nil];

我按以下方式在 ScrollView 上添加 View

for ( i = 0 ; i< 5; i++) 
{

UIView *userView = [classArray objectAtIndex:i];
CGRect rect;
rect.size.height = KVIEWHGT;
float userViewWidth = userView.frame.size.width;
rect.size.width = userViewWidth;
userView.frame = rect;
[scrollView addSubview:userView];
[userView release];

}

UIView *userView = nil;
NSArray *userSubViews = [scrollView subviews];
CGFloat x = 0;
float userViewWidth = 0.0f;
for (userView in userSubViews) {
if ([userView isKindOfClass:[UIView class]]){
CGRect frame = userView.frame;
frame.origin = CGPointMake(x, 0);
userView.frame = frame;
userViewWidth = userView.frame.size.width;
x += userViewWidth + 10;
}
}

[scrollView setContentSize:CGSizeMake(x, scrollView.frame.size.height)];

我在 ScrollView 上仅获得两个自定义 View 的输出,但我希望 ScrollView 上应该有 5 个自定义 View

请帮我解决这个问题。

最佳答案

您在数组中多次添加firstView、secondView,即使您多次添加它,它引用的是同一个实例

 NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView,  nil];

关于iphone - 将 View 添加到 ScrollView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8587788/

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