Ios 集合视图中的分页未正确居中

Ios 集合视图中的分页未正确居中,ios,uicollectionview,Ios,Uicollectionview,我有一个启用分页的UICollectionView,水平移动的各个视图没有正确居中。 我已确保视图与屏幕的宽度相同 关于如何强制UICollectionView水平分页视图的任何指针?您必须确保您的节插入+行距+单元格宽度都完全等于CollectionView的边界。我对自定义UICollectionViewFlowLayout子类使用以下方法: - (void)adjustSpacingForBounds:(CGRect)newBounds { NSInteger count = newB

我有一个启用分页的UICollectionView,水平移动的各个视图没有正确居中。 我已确保视图与屏幕的宽度相同


关于如何强制UICollectionView水平分页视图的任何指针?

您必须确保您的节插入+行距+单元格宽度都完全等于CollectionView的边界。我对自定义UICollectionViewFlowLayout子类使用以下方法:

- (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的一个子类,因此它不知道您希望分页如何工作。在UICollectionView之前,在布局UIScrollView的子视图时,您必须处理所有这些数学问题。

覆盖该方法:

- (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];
}
-(void)ScrollViewWillendDraging:(UIScrollView*)带速度的scrollView:(CGPoint)速度targetContentOffset:(inout CGPoint*)targetContentOffset{
*targetContentOffset=scrollView.contentOffset;//将加速度设置为0.0
float pageWidth=(float)self.articlesCollectionView.bounds.size.width;
int minSpace=10;
int cellToSwipe=(scrollView.contentOffset.x)/(pageWidth+minSpace)+0.5;//单元格宽度+行的最小间距
if(cellToSwipe<0){
cellToSwipe=0;
}else if(cellToSwipe>=self.articles.count){
cellToSwipe=self.articles.count-1;
}
[self.articlesCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:CellToSweep-inSection:0]在ScrollPosition:UICollectionViewScrollPositionLeft动画:是];
}

您解决了这个问题吗?我也有同样的问题。细胞偏离中心,当我滚动偏离中心的化合物使情况变得更糟时。@IkegawaTaro这里也有同样的问题。你解决过吗?我把这个方法放在哪里?什么时候/在哪里叫它?