为什么UIScrollView不滚动?

时间:2012-10-19 11:57:16

标签: ios xcode uiscrollview

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

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

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

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

修改 当我在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不会让步!

1 个答案:

答案 0 :(得分:2)

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

scrollView.contentSize = ...

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

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