How do I set collection view's cell size via the auto layout

前端 未结 5 616
逝去的感伤
逝去的感伤 2020-12-05 07:45

Hi I\'m trying to resize the cell via the auto layout.

I want display the cell by the 3 by 3.

First Cell\'s margin left=0

Last Cell\'s m

5条回答
  •  难免孤独
    2020-12-05 08:16

    Swift 3 version code based on vacawama answer:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
            let cellsAcross: CGFloat = 3
            let spaceBetweenCells: CGFloat = 1
            let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
    
            return CGSize(width: dim, height: dim)
        }
    

提交回复
热议问题