2013-02-27 86 views
1

我有一個UIScrollview包含默認高度爲1717px的數據,在運行時我必須添加一個或多個子視圖,每個子視圖540px高。事先未知其中有多少將不得不被添加。UIScrollview重置內容

我添加這些在viewdidload方法滾動視圖,然後設置contentsize爲正確的值。我還將contentize設置爲任何我需要的值。如果我在方法結尾記錄它,一切都很好,contentsize仍然正確。

一旦顯示contentsize已經恢復到1717px,內容仍然存在。如果我滾動到uiscrollview的正常範圍之外,我可以查看第二秒添加的內容,但它會反彈回來。

viewdidload的相關部分低於

NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"counter" ascending:YES]]; 
for (Facade * f in [self.inspection.facades sortedArrayUsingDescriptors:sortDescriptors]) { 
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"facadeView" owner:self options:nil]; 
    facadeView *facadeInfo = [subviewArray objectAtIndex:0]; 
    facadeInfo.facade = f; 
    facadeInfo.frame = CGRectMake(0, self.scrollView.frame.size.height, facadeInfo.frame.size.width, facadeInfo.frame.size.height); 
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height+facadeInfo.frame.size.height); 
    self.scrollView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height+facadeInfo.frame.size.height); 
    [self.scrollView addSubview:facadeInfo]; 
} 
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);; 
// now add our scroll view to the main view 
[self.view addSubview:self.scrollView]; 

我能做些什麼,以從重置爲初始值停止我contentsize

回答

0

我覺得

self.scrollView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height+facadeInfo.frame.size.height);

裏面的for循環的問題。你不需要增加幀的大小。如果你這樣做,你不能滾動。 contentize必須小於frame size。如果不是這樣,則滾動視圖不能滾動。

+0

滾動視圖的大小是好的,在循環後它會變回正確的值。它主要用於跟蹤內容必須在循環的下一次迭代中的大小。我可以在那裏更改代碼,以便不使用框架大小來計算我的內容大小,但是當我編寫代碼時沒有看到選項。 – Arperum 2013-02-27 19:13:27

1

請嘗試在viewDidAppear而不是viewDidLoad此代碼。

-1

這是autoLayout的問題。禁用自動佈局,它會工作正常!!! :-)