2012-10-19 62 views
0

我有一個UIScrollView,裏面有一些UILabels和一個UITextView。 UIScrollView是主UIView的子視圖。爲什麼UIScrollView不滾動?

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

的UILabels被添加到它作爲子視圖::作爲 的UIScrollView中被初始化

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的被添加作爲一個黑客(shown here),並且基本上是唯一的一個可滾動,所述其他視圖保持不變,我需要它們滾動以及。

編輯: 當我甚至添加一個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]; 

滾動視圖不會讓步!

+0

你需要設置scrollView的'contentSize'。此外,你應該考慮使用ARC(自動引用計數),因爲這會讓你的應用更可靠。 –

回答

2

你應該嘗試設置滾動視圖的contentSize

scrollView.contentSize = ... 

到的東西比滾動視圖幀更大。 (比如說,包含標籤和文本視圖的東西,可能高達1160像素)。

應將滾動視圖的框架設置爲表示滾動視圖中可見區域的大小,而不是滾動視圖內容的完整大小。