我做了一些研究,但是我找不到任何关于如何在UICollectionView中水平居中单元格的代码示例.
而不是第一个单元格像这个X00,我希望它像这个0X0.有没有办法实现这个目标?
编辑:
想象我想要的东西:
当CollectionView中只有一个元素时,我需要它看起来像版本B. 当我有多个元素时,它应该像版本A但有更多元素.
当我只有1个元素时,它看起来像版本A,我想知道我怎么能让它看起来像B.
谢谢您的帮助!
使用库不是一个好主意,如果你的目的只是这个,即中心对齐.
更好的是,您可以在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 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
试试这个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
我使用KTCenterFlowLayout来做这件事,效果很好.它是一个自定义子类,UICollectionViewFlowLayout
可以根据需要使单元格居中.(注意:通过发布一些代码来解决这个问题并非易事,这就是为什么我要链接到GitHub项目!)
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; }
略微修改@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) }