UICollectionView 3 column grid, 1px space?? (image included)

前端 未结 4 457
陌清茗
陌清茗 2020-12-31 04:04

I have setup a UICollectionView with a FlowLayout that leaves no space in between cells in either the vertical or horizontal directions.

I have everything working, y

4条回答
  •  旧时难觅i
    2020-12-31 04:23

    For example you have screen width = 320, when you divide it by 3 you have 106.6666. If consider it as Int, we have: 106 * 3 = 318. We need 2 pixel to be added by 1 px to first two cells because 320 - 318 = 2. Try this solution below.

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let columns = 3 
        let width = Int(UIScreen.main.bounds.width)
        let side = width / columns
        let rem = width % columns
        let addOne = indexPath.row % columns < rem
        let ceilWidth = addOne ? side + 1 : side
        return CGSize(width: ceilWidth, height: side)
    }
    

提交回复
热议问题