2013-03-04 93 views
6

我有一個啓用了分頁的UICollectionView,並且水平移動的各個視圖沒有正確居中。 我確保視圖與屏幕寬度相同。在集合中查看查看不正確集中

關於如何強制UICollectionView水平頁面視圖的任何指針?

+1

你解決這個問題?我有同樣的問題。這些細胞偏離中心,因爲我滾動偏心的化合物使其變得更糟。 – IkegawaTaro 2013-04-20 05:40:31

+0

@IkegawaTaro同樣的問題在這裏。你有沒有解決它? – 2016-04-23 19:20:02

回答

3

您必須確保您的部分插入+行間距+單元格寬度全部等於CollectionView的邊界。我用一個自定義UICollectionViewFlowLayout sublcass以下方法:

- (void)adjustSpacingForBounds:(CGRect)newBounds { 
    NSInteger count = newBounds.size.width/self.itemSize.width - 1; 

    CGFloat spacing = (newBounds.size.width - (self.itemSize.width * count))/count; 

    self.minimumLineSpacing = spacing; 

    UIEdgeInsets insets = self.sectionInset; 
    insets.left = spacing/2.0f; 
    self.sectionInset = insets; 
} 

你要記住,UICollectionView只是一個UIScrollView的sublcass所以它不知道你是怎麼想的分頁工作。在UICollectionView之前,您必須在佈置UIScrollView的子視圖時處理所有這些數學運算。

+0

我在哪裏放置這種方法?何時/我在哪裏打電話? – 2016-04-23 19:18:42

0

覆蓋的方法:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 
    *targetContentOffset = scrollView.contentOffset; // set acceleration to 0.0 
    float pageWidth = (float)self.articlesCollectionView.bounds.size.width; 
    int minSpace = 10; 

    int cellToSwipe = (scrollView.contentOffset.x)/(pageWidth + minSpace) + 0.5; // cell width + min spacing for lines 
    if (cellToSwipe < 0) { 
     cellToSwipe = 0; 
    } else if (cellToSwipe >= self.articles.count) { 
     cellToSwipe = self.articles.count - 1; 
    } 
    [self.articlesCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:cellToSwipe inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; 
}