0

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

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

UILabels 作为子视图添加到其中:

    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 添加的(如图所示),基本上是唯一一个可滚动的,其他视图保持静态,我也需要它们滚动。

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

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

滚动视图不会让步!

4

1 回答 1

2

您应该尝试设置contentSize滚动视图:

scrollView.contentSize = ...

比滚动视图框架更大的东西。(比如说,包含标签和文本视图的东西,可能是 1160 像素高)。

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

于 2012-10-19T12:03:27.310 回答