简体   繁体   中英

Distribution of free space between collection view cells

I'm trying to use collection view as week days picker.

在此输入图像描述

Height and width of cell are constant. I'd like to position cells to fill full collection view width. I've tried this but doesn't work for me:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        let collectionWidth = collectionView.frame.width
        let freeSpace = collectionWidth - 210

        return freeSpace / 7
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 30, height: 30)
    } 

You need

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    let collectionWidth = (collectionView.frame.width - numberOfItems * 30 ) / (numberOfItems - 1)
     return collectionWidth
}

where numberOfItems = array datasource count of the collection


You may need

return freeSpace / 6

instead of

return freeSpace / 7

as you need to divide by numOfelements - 1 which is the correct count of spaces between the 7 items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM