2012-08-29 47 views
0

在我看來位指示的viewDidLoad功能,我有這樣的傢伙:爲什麼這個UIScrollView實驗失敗?

UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.frame]; 
scroller.contentSize = self.view.frame.size; // Tried w/ and w/o this 
scroller.showsVerticalScrollIndicator = YES; // Tried w/ and w/o this 

for (int x = 0; x < 10; x++) { 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, x * 100, 100, 100)]; 
    label.text = [NSString stringWithFormat:@"%i label", x]; 
    [scroller addSubview:label]; 
} 

[self.view addSubview:scroller]; 

它顯示了前8個標籤OK,但滾動視圖不會...滾動。它只是被切斷。任何想法爲什麼?

回答

0

它不會滾動,因爲您的contentSize只設置爲與滾動視圖本身大小相同。 contentSize是滾動視圖用作窗口的可滾動區域的大小。您需要將其設置得足夠寬才能看到您的標籤。

根據您當前的代碼,您的最終標籤爲{0, 900, 100, 100},因此您的contentSize需要至少爲CGSizeMake(100, 1000)

+0

同樣有用的是設置'contentSize'與我添加到它的對象的數量成比例。謝謝! – Wells