Aligning collection view cells to fit exactly 3 per row

前端 未结 5 1738
深忆病人
深忆病人 2020-12-23 22:01

I am trying to make a collection view of images, so that there are 3 per row, with no spacing in between.

My collection view data sources are:

func n         


        
5条回答
  •  遥遥无期
    2020-12-23 23:02

    Swift - Version 4.2

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        let noOfCellsInRow = 3
    
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    
        let totalSpace = flowLayout.sectionInset.left
            + flowLayout.sectionInset.right
            + (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
    
        let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
    
        return CGSize(width: size, height: size) 
    }
    

提交回复
热议问题