Vladimir Zhelnov Vladimir Zhelnov - 11 months ago 69
iOS Question

Resizing CollectionViewCell in UIView

I have CollectionView in UIView. How to resize CollectionViewCell to 50% of screen width? I want to create 2 columns in CollectionView.

class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate

Also i have image and label in CollectionViewCell.

Answer Source

You can use sizeForItemAt method of UICollectionView to manage cell size of it

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
    return CGSize(width: screenWidth/2, height: yourHeight)

Also confirms to UICollectionViewDelegateFlowLayout