gpt4 book ai didi

ios - 为什么 UIScrollView 不滚动?

转载 作者:行者123 更新时间:2023-11-28 22:46:57 24 4
gpt4 key购买 nike

我有一个 UIScrollView,里面有一些 UILabel 和一个 UITextView。UIScrollView 是主 UIView 的 subview 。UIScrollView 被初始化为:

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 75, rct.size.width-50, 1000)];

UILabel 作为 subview 添加到它:

    lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:[UIColor greenColor]];
[lbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16]];
lbl.text = [NSString stringWithFormat:@"name: %@",firstName];
[scrollView addSubview:lbl];
[lbl release];

lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 40)];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:[UIColor redColor]];
[lbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16]];
lbl.text = [NSString stringWithFormat:@"last name: %@",lastName];
[scrollView addSubview:lbl];
[lbl release];

.
.

// TEXT VIEW

UITextView *txt1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 160, rct.size.width-50, 1000)];
txt1.textAlignment = UITextAlignmentLeft;
txt1.textColor = [UIColor whiteColor];
[txt1 setBackgroundColor:[UIColor clearColor]];
UIFont *f = [UIFont fontWithName:@"Arial-BoldMT" size:16];
[txt1 setFont:f];
txt1.text = [NSString stringWithFormat:@"info: %@",personInfo];
[scrollView addSubview:txt1];

CGRect newframe1 = txt1.frame;
newframe1.size.height = txt1.contentSize.height + 3; // offset for shadow
txt1.frame = newframe1;
[txt1 release];

TextView 是作为 hack(shown here)添加的,基本上是唯一可滚动的,其他 View 保持静态,我也需要它们滚动。

编辑:当我在 scrollView 的边界之外添加一个 UILabel 时,它不会滚动到那里:

    lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 940, 100, 1000)];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:[UIColor whiteColor]];
[lbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
lbl.text = [NSString stringWithFormat:@"TEST"];
[scrollView addSubview:lbl];
[lbl release];

scrollView 不会移动!

最佳答案

您应该尝试设置 ScrollView 的 contentSize:

scrollView.contentSize = ...

比 ScrollView 框架更大的东西。 (比如说,包含标签和 TextView 的东西,可能有 1160 像素高)。

ScrollView 的框架应设置为表示 ScrollView 中可见区域的大小,而不是 ScrollView 内容的完整大小。

关于ios - 为什么 UIScrollView 不滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12973796/

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