UICollectionView Custom Cell to fill width In Swift

后端 未结 11 2020
不思量自难忘°
不思量自难忘° 2021-01-30 21:12

I\'ve been trying to figure-out how can i make the cell fill the width, as you can see in the picture the width between the cells is too big. i am using custom cell with only on

11条回答
  •  梦毁少年i
    2021-01-30 21:51

    For my case, I wanted to show two columns and 3 rows in the UICollectionView

    I added UICollectionViewDelegateFlowLayout delegate to my class

    then I override sizeForItemAt indexPath

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellHeight = (collectionView.bounds.size.height - 30) / 3 // 3 count of rows to show
        let cellWidth = (collectionView.bounds.size.width - 20) / 2 // 2 count of colomn to show
        return CGSize(width: CGFloat(cellWidth), height: CGFloat(cellHeight))
    }
    

    The 30 is the Line spacing, the 20 is the insent between cells

提交回复
热议问题