UICollectionViewCell gets resized by UIImageView

Hello!



I've noticed a very strange behavior with UICollectionViewCell.

I am currently using XCode 11.


I have an UICollectionView and a simple UICollectionViewCell with an imageview inside.

The Imageview has four constraints Trailing, Leading, Bottom and Space to Superview.

The delegates are implemented:


extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collCell", for: indexPath)
        cell.backgroundColor = data[indexPath.row]
       
        return cell
    }
}

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

This

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

should create cells with the size 200x200. Which is working fine unless i add a image to the imageview within UICollectionViewCell.

Then the cell gets the size of that image and is no longer 200x200


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collCell", for: indexPath)
        cell.backgroundColor = data[indexPath.row]
        cell.imageView.image = UIImage(named: "test")
        return cell
    }


How to prevent the imageview to resize the uicollectionviewcell?


Thank you

Replies

I would define size constraints (in IB) on the imageView itself: 200 * 200.


Does it work for you ?

Well, this should work, but now i need update all constraints for all images, set the constants of all constraints instead of just setting width and height of the cell.


So this is no solution.


It also happens with labels.

The collectionview definitely ask the delegate for the itemsize, but resets it to the contentsize of its label in the cell.


Is there any new option i need to uncheck?!

This behavior is strange and doesn’t exist in older xcode versions.

Well i fixed it by installing xcode 10.3 again,

building the storyboard and then open the project in xcode 11 and never touch the Collectionview again.

There is something terrible broken. In xcode 10.3 everything works fine.


It has something todo with the ContentView of the UICollectionViewCell and it only affects CollectionViewCells which are added in xCode 11.

CollectionViewCells which were added prior to xcode 11 are working fine.


I can disable "Content View" within the Size-Inspector of the CollectionViewCell, which is new, but autolayout doesn't work anymore if you disable "Content View"

I had this exact same problem, but setting the imageView.image to nil in prepareForReuse() resolved it for me.


Very strange.

Apparently it's not a bug, it's a feature 😐

UICollectionView cells can now self-size, and happily do that on their own unless you tell them not to.


To opt out this behaviour, set Estimated Size of cells to None, in Xcode (on the UICollectionView property tab).

Here's where I found the solution.

https://stackoverflow.com/questions/56840665/why-on-xcode-11-uicollectionviewcell-changes-size-as-soon-as-you-scroll-i-alre


Cheers,

Davide

Add a Comment