title: UICollectionView items disappear when scrolling with my own UICollectionViewFlowLayout subclass
body: <p>Context: I am using an <code>UICollectionView</code> for a photoview. Every picture is a cell with one <code>UIImage</code>. Images can have different sizes and I want them to fill the whole screen.
  So I wrote a class who determines the frame of every single <code>UICollectionCell</code> and let a subclass of <code>UICollectionViewFlowLayout</code> ask that class for the right frame for every item.</p>
  
  <p>My implementation of the <code>UICollectionViewFlowLayoutClass:</code></p>
  
  <pre><code>override func layoutAttributesForElementsInRect(rect: CGRect) -&gt; [AnyObject]? {
      let attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]
      for attributes in attributesToReturn ?? [] {
          if attributes.representedElementCategory == .Cell {
              let frame = self.layoutAttributesForItemAtIndexPath(attributes.indexPath).frame
              attributes.size = frame.size
              attributes.frame = frame
          }
      }
      return attributesToReturn
  }
  
  override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -&gt; UICollectionViewLayoutAttributes! {
      let curAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)
      let frame = mazeManager.sizeForItemAtIndex(indexPath, collectionView: collectionView!)
      curAttributes.size = frame.size
      curAttributes.frame = frame
      return curAttributes
  }
  </code></pre>
  
  <p>So the frame asks my MazeManager to give back a frame. The returned frames seem to be correct and they all fit in the UICollectionView.</p>
  
  <p>When I open my app everything looks fine, even when I scroll. But when I scroll to a specific position (this position feels random because it depends on the images I test with, but with the same set of images the positions are the same) cells disappear from my view. When I scroll back they return.</p>
  
  <p>I've checked if the cells where not hidden, but they never are.</p>
  
  <p>On some other threads with similar issues the answer is to implement the <code>collectionViewContentSize</code> so I did:</p>
  
  <pre><code>override func collectionViewContentSize() -&gt; CGSize {
      let size = mazeManager.collectionViewContentSize
      return size.height &lt; collectionView!.frame.size.height ? collectionView!.frame.size : size
  }
  </code></pre>
  
  <p>The number of items is not static and it grows while reaching the end of the view. So what happens here is:
  The manager determines the Origin.y + the Size.Height of the last item (+ 10 points to be sure), the width is the width of the UICollectionView.</p>
  
  <p>Still all the frames of the cells are within the sizes returned by this method. But still some cell disappear or never appear at all.</p>
  
  <p>When I scroll further through the UICollectionView I see other cells which are positioned on the right place. So there are gaps in my view, but the flow continues. (When a gap appears there are no calls for the items at the missing idexpaths in the collectionViewDelegate. For example the CollectionView asks for items: 1,2,3,4,5,6, 12,13,14).</p>
  
  <p>The console prints nothing about wrong positioning, I've checked and everything is within the ContentSize. So I'm almost out of options. Can anybody explain what's happening in my case?</p>
  
  <p>Thank you.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>While I was looking for a solution, I already found the mentioned post (<a href="https://stackoverflow.com/questions/13360975/uicollectionviews-cell-disappearing">UICollectionView&#39;s cell disappearing</a>) and I already did 3 of the 4 steps.</p>
  
  <pre><code>override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -&gt; Bool {
      return true
  }
  </code></pre>
  
  <p>And I just added the scroll direction in the initializer:</p>
  
  <pre><code>override init(){
      super.init()
      scrollDirection = .Vertical
  }
  
  required init(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
      self.scrollDirection = .Vertical
  }
  </code></pre>
  
  <p>Unfortunately this doesn't fix my issue, so it doesn't seem a duplicate to me.</p>