56

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Reza.Ab
  • 1,195
  • 1
  • 11
  • 21
  • I have face the same issue in Xcode 11. Is there any solution ? – Anand Nanavaty Aug 05 '19 at 08:34
  • 1
    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 @AnandNanavaty – Reza.Ab Aug 18 '19 at 22:12
  • 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 Oct 01 '19 at 11:12
  • 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 Oct 01 '19 at 12:31

4 Answers4

173

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

enter image description here

Tuan Nguyen
  • 2,624
  • 1
  • 17
  • 15
  • Wow...this was it! Thank you very much. I had the issue when adding an image to an `UIImageView` it always stretched the cell to the size of the image. – gpichler Jan 05 '20 at 22:24
  • 5
    You are a life saver :) For the ones who try to set none in code; i set it to .zero "layout.estimatedItemSize = .zero" – ergunkocak Feb 06 '20 at 17:22
  • It works for me It really helps me for fixing image view bounds to the cell frames. Does any one know how to use this Estimate Size stuff in collection view? – shashi Gupta Feb 18 '20 at 05:20
  • I was looking for this for a while and this fixed my issue! Thanks! – Matt Carroll May 23 '20 at 18:43
  • Does not work for me. I have a horizontal scrolling collection view and setting `.zero` makes all the widths constant which is not what I want. https://stackoverflow.com/questions/65681344/horizontal-scrolling-collection-view-shifts-up-when-scrolled-in-parent-container/65681733?noredirect=1#comment116129075_65681733 – Parth Jan 12 '21 at 10:20
  • I was in such a difficult situation. This error taken all day. Thank you so much. – Alperen ARICI May 26 '21 at 15:50
19

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

tetrajen
  • 508
  • 6
  • 10
11

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
Carlos Irano
  • 682
  • 7
  • 8
7

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

Talha Rasool
  • 1,126
  • 14
  • 12