52
votes

enter image description here

I have collectionview inside tableview cell and I use nib for my collection view cell (in which I use autolayout for my imageview and labels and it is on Freeform mode). I'm setting cell size in tableviewcell class which is handling the delegate for collectionview inside it by this method:

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

It always works but after Xcode 11 it doesn't.

4
I have face the same issue in Xcode 11. Is there any solution ?Anand Nanavaty
yep, just had to remove all the constraints by autolayout and if you have any, just make an outlet to your class and set it by code, thats the only work around if you are using autolayout, otherwise you have to use Xcode 10.2 @AnandNanavatyReza.Ab
Same here. I noticed that the UICollectionViewCells gets resized by its content. If you put an UIImageView inside the cell and assign an image, the cell gets resized even if you implemented the delegate methods. Same goes fo UILabel, etc. This only seems to happen if you use Top, Bottom, Trailing and Leading Constraints to fill something up. If you use fixed constraints like width and height it doesn't happen.EdFunke
It has something todo with the "ContentView" Setting within the Size-Inspector of the UICollectionViewCell. But disabling it breaks autolayout. So i fixed it by installing xCode 10 again and building the storyboard in xcode 10.EdFunke

4 Answers

158
votes

I have the same problem. And my solution is to change the Estimate size to None in the Xcode 11.

enter image description here

17
votes

You set collectionview Estimate size to None in Xcode 11. The reason for this is that cells in a UICollectionView can now self-size with Auto Layout constrained views in the canvas. To opt into the behavior for existing collection views, enable “Automatic” for the collection view’s estimated size, and “Automatic” for cell’s size from the Size inspector.

If deploying before iOS 13, you can activate self sizing collection view cells by calling performBatchUpdates(_:completion:) during viewDidLoad()

Ref: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_release_notes

8
votes

As Anh Tuan said in another answer here, you just need to change the Estimate Size to none in the Size Inspector of the Collection View from the Storyboard.

But if you wanna do this programmatically, you can try this code:

    let layout = myCollectionViewReferenceHere.collectionViewLayout as! UICollectionViewFlowLayout
    layout.estimatedItemSize = .zero
7
votes

This problem is coming in Xcode 11. Go to the attribute inspector and change the estimateSize to None. will fix every thing.