Home > Enterprise >  UICollectionView Change Cell size after start scrolling
UICollectionView Change Cell size after start scrolling

Time:09-28

So I have UICollectionView on my Controller UICollection view consist of two sections. So when I start scrolling, it changes cell sizes

enter image description here

My code for layout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let section = indexPath.section
    let row = indexPath.row
    if section == 0 {
        if (row == 0){
            let width = view.frame.width
            return.init(width: width, height: 120)
        }else{
            let width = view.frame.width
            return.init(width: width, height: 50)
        }
    }else{
        //
        //return.init(width: 200, height: 120)
     
        let width = collectionView.bounds.width / 2 - 10
        
        return.init(width: width, height: 253)
    }
    
    
}

Producs are in second section, but it looks like it gives a width from first section

CodePudding user response:

Your error the first size for section 0 but anther section you changeded the cell size you can use this code :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = collectionView.bounds.width / 2 - 10
    let height = collectionView.bounds.height
    return.init(width: width, height: height)

}

CodePudding user response:

Make sure estimateSize is set to NONE in storyboard.enter image description here

  • Related