UICollectionView: remove space between cells (with 7 items per row)

前端 未结 1 528
心在旅途
心在旅途 2021-01-28 06:50

I have a UICollectionView with 7 items per row (or actually, the width of each item is the collectionView.bounds.width divided by 7).

Here is a

1条回答
  •  一整个雨季
    2021-01-28 07:03

    You have to somehow distribute the additional pixels. Don't worry that all cells won't have exactly the same width, 1px difference won't be visible:

    let numColumns = 7
    let availableWidth = collectionView.bounds.size.width
    let minWidth = floor(availableWidth / CGFloat(numColumns))
    let remainder = availableWidth - minWidth * CGFloat(numColumns)
    

    Now what to do with the remainder? Let's just add pixels to cell from the left:

    let columnIndex = indexPath.row % numColumns
    let cellWidth = CGFloat(columnIndex) < remainder ? minWidth + 1 : minWidth
    return CGSize(width: cellWidth, height: 45)
    

    This is just the basic idea.

    0 讨论(0)
提交回复
热议问题