title: UICollectionView autosizing cells break UICollectionViewFlowLayout settings
body: <p>I'm currently working on autosizing cells which are new in <code>iOS8</code>. </p>
  
  <p>According to WWDC 2014 video 'What's New in Table and Collection Views', all that is required to autosize cells is to set an <code>estimatedSize</code> on the <code>UICollectionViewFlowLayout</code> given to the <code>UICollectionView</code> on initialisation, and override the <code>sizeThatFits:</code> method in the <code>UICollectionViewCell</code>.</p>
  
  <p><strong>Initialisation</strong>:</p>
  
  <pre><code>UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  
  [flowLayout setEstimatedItemSize:CGSizeMake(146.0f, 156.0f)];
  
  _collectionView = [[UICollectionView alloc] initWithFrame:[_containerView bounds] collectionViewLayout:flowLayout];
  </code></pre>
  
  <p><code>sizeThatFits</code> method in my cell, where _userLabel is the label that is on the bottom and marks the actual height.</p>
  
  <pre><code>- (CGSize)sizeThatFits:(CGSize)size
  {
      CGSize actualSize = CGSizeMake([[self contentView] bounds].size.width, [_userLabel frame].origin.x + [_userLabel bounds].size.height);
  
      return actualSize;
  }
  </code></pre>
  
  <p>It turns out that actual size has larger height than my estimated size, which should be okay and the cell actually gets resized properly in height.</p>
  
  <p>Now with the iPhone6 and 6+, the width can also differ. I want the cells to properly span the width of the screen. I've therefore implemented three delegate methods on UICollectionViewFlowLayout; my first intuition is to calculate the width I want the cells to have according to the screen width, the section insets and the inter-item spacing.</p>
  
  <pre><code>- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      UIEdgeInsets sectionInsets = [self collectionView:collectionView layout:collectionViewLayout insetForSectionAtIndex:[indexPath section]];
      CGFloat interItemSpacing = [(UICollectionViewFlowLayout *) collectionViewLayout minimumInteritemSpacing];
  
      CGFloat cellWidth = ([_collectionView bounds].size.width - sectionInsets.left - sectionInsets.right - interItemSpacing) / 2;
  
      CGSize collectionViewCellSize = CGSizeMake(cellWidth, 156.0f);
  
      return collectionViewCellSize;
  }
  
  - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  {
      return 12.0f;
  }
  
  - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  {   
      return UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
  }
  </code></pre>
  
  <p>It works, however partly. The collectionView won't respect the top section inset, and when scrolling the collectionView, switching out another tab and then back again, cells seem to have disappeared.</p>
  
  <p>Apparently I'm doing something wrong, but I can't seem to figure out where. Anyone got a clue?</p>