title: UICollectionView autosize height
body: <p>How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling <code>reloadData</code> and invalidating the layout:</p>
  
  <pre><code>self.collectionView.contentSize = CGSizeMake(300, 2000);
  self.collectionView.frame = CGRectMake(0, 0, 300, 2000);
  [self.collectionView reloadData];
  [self.collectionView.collectionViewLayout invalidateLayout];
  </code></pre>
  
  <p>but none of this has any effect. After pressing the button I still see the initial view, like this:</p>
  
  <p><img src="https://i.stack.imgur.com/Wlzkc.png" alt="collection view only showing part of the content after frame resize"></p>
  
  <p>I have a small demo program where I have a data source producing 100 elements. In Interface Builder I initially set the size of the UICollectionView to a small value so that not all elements fit, after that I press a button after which the code above is executed. I expect the UICollectionView to now show all elements, but it doesn't.</p>
  
  <p><strong><em>EDIT:</strong> The demo program can be found at <a href="https://github.com/mjdemilliano/TestUICollectionView" rel="noreferrer">https://github.com/mjdemilliano/TestUICollectionView</a>.</em></p>
  
  <p><strong><em>EDIT2:</strong> I have observed that the frame update is lost at some point, because if I press the button again, the current frame is back to the old value. After adding some log statements in the button event handler, the log output is:</em></p>
  
  <pre><code>before: frame = {{0, 58}, {320, 331}}, contentSize = {320, 1190}
  update button pressed
  after: frame = {{0, 0}, {300, 2000}}, contentSize = {300, 2000}
  before: frame = {{0, 58}, {320, 331}}, contentSize = {320, 1190}
  update button pressed
  after: frame = {{0, 0}, {300, 2000}}, contentSize = {300, 2000}
  </code></pre>
  
  <p><em>I don't understand why the frame change is not kept, what is changing it.</em></p>
  
  <p>At some point I will replace the hardcoded values by values obtained from the flow layout, but I wanted to rule that out and keep my example as simple as possible.</p>
  
  <p>Context: What I want to do eventually is the following: I have a scrollable view with various controls like labels and images, and a collection view with dynamic content. I want to scroll all that, not just the collection view, therefore I am not using the collection view's own scrolling facilities, which work fine.</p>