使集合视图单元格居中

时间:2019-01-31 15:25:13

标签: objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

enter image description here

我想使用UICollectionView创建以下视图。我尝试不同的东西将一个项目放在中心,但没有成功。我尝试用滚动确实结束,使它居中,但它有一些奇怪的效果。谁能帮我把这个物品放在中心吗?

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint centerPoint = CGPointMake(self.centeredCollectionView.center.x + self.centeredCollectionView.contentOffset.x,
                                      self.centeredCollectionView.center.y + self.centeredCollectionView.contentOffset.y);
    NSIndexPath *centerCellIndexPath = [self.centeredCollectionView indexPathForItemAtPoint:centerPoint];
    [self.centeredCollectionView scrollToItemAtIndexPath:centerCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat widthHeight = collectionView.frame.size.width - 70;

    return CGSizeMake(widthHeight, collectionView.frame.size.height);
}

0 个答案:

没有答案