2011-06-29 59 views
34

我調整的細節視圖控制器的狀態,就在它上的navigationController被推:的UIScrollView調整contentOffset當contentSize改變

[self.detailViewController detailsForObject:someObject]; 
[self.navigationController pushViewController:self.detailViewController 
            animated:YES]; 

在滾動視圖駐留的DetailViewController。其內容我調整基於傳遞的對象上:

- (void)detailsForObject:(id)someObject { 
    // set some textView's content here 
    self.contentView.frame = <rect with new calculated size>; 

    self.scrollView.contentSize = self.contentView.frame.size; 
    self.scrollView.contentOffset = CGPointZero; 
} 

現在,這一切工作,但滾動視圖調整它在navigationController的幻燈片的動畫contentOffsetcontentOffset將被設置爲最後一個contentSize和新計算的之間的差異。這意味着第二次打開detailsView時,細節會滾動到一些不需要的位置。儘管我明確地將contentOffset設置爲CGPointZero

我發現重置contentOffset- viewWillAppear沒有效果。我能想出的重置在viewDidAppear的contentOffset,導致內容的顯着的上下運動的最佳:

- (void)viewDidAppear:(BOOL)animated { 
    self.scrollView.contentOffset = CGPointZero; 
} 

有沒有一種方法,以防止UIScrollView從調整其contentOffset當其contentSize改變?

+0

爲什麼要調整動畫塊內的內容偏移量?跳過CATransaction的東西,所以它不動畫。然後它不會看起來波濤洶涌。 – Andrew

+0

刪除CATransaction的東西確實不會改變一件事情,所以它被刪除。 – Berik

+0

你確定viewDidAppear被調用嗎?我會在其中設置一個斷點或一個NSLog語句。另外,您是否嘗試切換您的調用順序,即在推送後設置detailsForObject? – Andrew

回答

57

使用UINavigationControllerUIViewController含有UIScrollView時發生。

的iOS 7

溶液1(編號)

@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets設置到NO

溶液2(故事板)

取消選中​​

Adjust Scroll View Insets

iOS 6的

溶液(碼)

viewWillLayoutSubviews中設置UIScrollView的財產contentOffsetcontentInset。示例代碼:

- (void)viewWillLayoutSubviews{ 
    [super viewWillLayoutSubviews]; 
    self.scrollView.contentOffset = CGPointZero; 
    self.scrollView.contentInset = UIEdgeInsetsZero; 
} 
+1

這是解決問題的新方法。然後重新設置contentSize會更簡潔。 – Berik

+3

傑出的答案。感謝您在iOS 7中發佈差異,甚至包括圖片來澄清。超! – Marchy

+4

謝謝!在過去的幾個小時裏一直在堅持這個 – user2527666

0

你的scrollView是DetailViewController的根視圖嗎?如果是,請嘗試將scrollView包裝在普通的UIView中,並使後者成爲DetailViewController的根視圖。由於UIView沒有contentOffset屬性,因此它們不受導航控制器(由於導航欄等)所做的內容偏移調整的限制。

+1

scrollView不是DetailViewController的根視圖。 self.scrollView綁定到位於DetailViewController的根視圖中的UIScrollView。 – Berik

14

雖然我找到了解決方案,但問題的原因仍不清楚。通過重置內容的大小和調整之前偏移量,UIScrollView的將無法運行動畫:

- (void)detailsForObject:(id)someObject { 
    // These 2 lines solve the issue: 
    self.scrollView.contentSize = CGSizeZero; 
    self.scrollView.contentOffset = CGPointZero; 

    // set some textView's content here 
    self.contentView.frame = <rect with new calculated size>; 

    self.scrollView.contentSize = self.contentView.frame.size; 
    self.scrollView.contentOffset = CGPointZero; 
} 
0

我遇到的問題,並針對特定情況下 - 我不調整大小 - 我用下面的:

float position = 100.0;//for example 

SmallScroll.center = CGPointMake(position + SmallScroll.frame.size.width/2.0, SmallScroll.center.y); 

同樣會爲Y工作:anotherPosition + SmallScroll.frame.size.height/2.0

所以,如果你不需要調整,這是一個快速,無痛的解決方案。

2

我對UIScrollview有同樣的問題,其中問題是由於沒有設置contentSize造成的。將contentSize設置爲解決此問題的項目數量後。

self.headerScrollView.mainScrollview.contentSize = CGSizeMake(320 * self.sortedMaterial.count, 0); 
相關問題