当前位置:  开发笔记 > IOS > 正文

如何水平居中UICollectionView细胞?

如何解决《如何水平居中UICollectionView细胞?》经验,为你挑选了6个好方法。

我做了一些研究,但是我找不到任何关于如何在UICollectionView中水平居中单元格的代码示例.

而不是第一个单元格像这个X00,我希望它像这个0X0.有没有办法实现这个目标?

编辑:

想象我想要的东西:

在此输入图像描述

当CollectionView中只有一个元素时,我需要它看起来像版本B. 当我有多个元素时,它应该像版本A但有更多元素.

当我只有1个元素时,它看起来像版本A,我想知道我怎么能让它看起来像B.

谢谢您的帮助!



1> Darshan Pate..:

使用库不是一个好主意,如果你的目的只是这个,即中心对齐.

更好的是,您可以在collectionViewLayout函数中执行此简单计算.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

    let totalCellWidth = CellWidth * CellCount
    let totalSpacingWidth = CellSpacing * (CellCount - 1)

    let leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
    let rightInset = leftInset

    return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}


在Swift 3中,新方法签名是`collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,insetForSectionAt section:Int)`.
@DarshanPatel。您的答案有时会在较小的设备上产生负值。考虑像这样对您的leftInset值使用最大值检查:let let LeftInset = max(0.0,(self.collectionView.bounds.width-CGFloat(totalCellWidth + totalSpacingWidth))/ 2)`
这应该是公认的答案,因为它清晰简洁并解释了逻辑.
如果您不是子类化UICollectionViewController,请确保您的类符合UICollectionViewDelegateFlowLayout,否则它将无法正常工作

2> Ahmed Safadi..:

Swift 4.2

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    let totalCellWidth = 80 * collectionView.numberOfItems(inSection: 0)
    let totalSpacingWidth = 10 * (collectionView.numberOfItems(inSection: 0) - 1)

    let leftInset = (collectionView.layer.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
    let rightInset = leftInset

    return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)

}

斯威夫特3

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

        let totalCellWidth = 80 * collectionView.numberOfItems(inSection: 0)
        let totalSpacingWidth = 10 * (collectionView.numberOfItems(inSection: 0) - 1)

        let leftInset = (collectionView.layer.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
        let rightInset = leftInset

        return UIEdgeInsetsMake(0, leftInset, 0, rightInset)

    }

不要忘记添加协议

UICollectionViewDelegateFlowLayout



3> oscar castel..:

试试这个Swift 4吧

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let cellWidth : CGFloat = 165.0

        let numberOfCells = floor(self.view.frame.size.width / cellWidth)
        let edgeInsets = (self.view.frame.size.width - (numberOfCells * cellWidth)) / (numberOfCells + 1)

        return UIEdgeInsetsMake(15, edgeInsets, 0, edgeInsets)
    }

添加您的cellWidth而不是165.0



4> TwoStraws..:

我使用KTCenterFlowLayout来做这件事,效果很好.它是一个自定义子类,UICollectionViewFlowLayout可以根据需要使单元格居中.(注意:通过发布一些代码来解决这个问题并非易事,这就是为什么我要链接到GitHub项目!)



5> Stunner..:

Darshan Patel的客观C版答案:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    CGFloat totalCellWidth = kItemWidth * self.dataArray.count;
    CGFloat totalSpacingWidth = kSpacing * (((float)self.dataArray.count - 1) < 0 ? 0 :self.dataArray.count - 1);
    CGFloat leftInset = (self.bounds.size.width - (totalCellWidth + totalSpacingWidth)) / 2;
    CGFloat rightInset = leftInset;
    UIEdgeInsets sectionInset = UIEdgeInsetsMake(0, leftInset, 0, rightInset);
    return sectionInset;
}



6> MXV..:

略微修改@Safad Funy的答案,这对于Swift和iOS的最新版本对我来说是有用的。在这种情况下,我希望单元格的宽度为集合视图大小的三分之一。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

  let totalCellWidth = Int(collectionView.layer.frame.size.width) / 3 * collectionView.numberOfItems(inSection: 0)
  let totalSpacingWidth = (collectionView.numberOfItems(inSection: 0) - 1)

  let leftInset = (collectionView.layer.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
  let rightInset = leftInset

  return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
}

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有