Aligning collection view cells to fit exactly 3 per row

前端 未结 5 1749
深忆病人
深忆病人 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:05

    you can use this

    This code is somehow written that you can change section inset or minimumInteritemSpacing and this calculate and resize with this parameters

    you can use this code or download project from Github

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

提交回复
热议问题