ios – How should I specify collectionView cell size?


I need my collection view cell to be sized depending on different conditions.

That is what I use now:

func collectionView(
    _ collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAt indexPath: IndexPath
) -> CGSize {
    calculateNeededItemSize()
}

func calculateNeededItemSize() -> CGSize {
    if tabsList.count > Constant.maximumFullscreenTabsCount {
        return UICollectionViewFlowLayout.automaticSize
    } else {
        return CGSize(width: determineItemWidthForFullscreen(), height: frame.height)
    }
}

func determineItemWidthForFullscreen() -> CGFloat {
    return UIScreen.main.bounds.width / CGFloat(tabsList.count)
}

When I specify this line of code ‘return UICollectionViewFlowLayout.automaticSize’ with some test block it works fine. But otherwise I get a fatal error.

Also, when I I indicate this line
‘layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize’
without method sizeForItemAt it works but I just need my cell to have a different size depending on condition.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img