title: UICollectionView incorrectly hiding cells during layout transition
body: <p><em>There are a few questions about <code>UICollectionView</code> bugs that hide cells, but I don't think this one has been covered. Most other questions seem related specifically to <code>UICollectionViewFlowLayout</code> and section insets, whereas I have seen this problem when transitioning between completely custom layouts.</em></p>
  
  <hr>
  
  <p>When performing an interactive layout transition, <code>UICollectionView</code> appears to be hiding cells that should not be hidden:</p>
  
  <p><img src="https://i.stack.imgur.com/6qe13.gif" alt="enter image description here"></p>
  
  <p><em>(Note cells 40 &amp; 45 in the bottom left disappearing during the transition)</em></p>
  
  <p>It seems to occur in a specific place: whenever the cell's layout attributes (returned from the transition layout's <code>layoutAttributesForElementsInRect:</code>) have a frame with a minX less than the passed in <code>rect</code>'s minX AND a maxY greater than <code>rect</code>'s maxY. In code:</p>
  
  <pre><code>- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  {
      NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect];
      for (UICollectionViewLayoutAttributes *attr in layoutAttributes) {
          if (CGRectGetMinX(attr.frame) &lt; CGRectGetMinX(rect) &amp;&amp;
              CGRectGetMaxY(attr.frame) &gt; CGRectGetMaxY(rect)) {
              NSLog("Element at index path %@ will disappear unexpectedly", attr.indexPath);
          }
      }
      return layoutAttributes;
  }
  </code></pre>
  
  <p>The transition layout returns the correct layout attributes, showing the cell's centre moving gradually offscreen. However, once it has passed the point mentioned above, the collection view seems to decide it is no longer visible (it is!) and sets the cell's <code>hidden</code> property to yes, ignoring it until its frame moves further within <code>rect</code>.</p>
  
  <p>I have put together a <a href="https://github.com/sfoulston/CollectionViewHidingCells" rel="noreferrer">sample project</a> that demonstrates this issue (the animation above is of this sample in action). It uses two simple flow layouts for simplicity, but I first noticed it when working on a transition between custom <code>UICollectionViewLayout</code> subclasses. Most of the code is just managing the interactive transition using a pinch gesture recogniser, and I'm quite sure there's nothing remarkable there.</p>
  
  <p>The problem doesn't seem to happen when using plain animated layout transitions (<code>setCollectionViewLayout:animated:</code>)... try running the sample project with slow animations, and tap on a cell - all cells animated as expected.</p>
  
  <p>This seems like a bug in <code>UICollectionView</code>'s implementation to me, but I'd really appreciate it if anybody knows of an effective workaround.</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>I have filed a bug report with Apple, and simultaneously opened a Technical Support Incident to see if a workaround can be found. I'll add an answer if I learn anything useful from the engineers.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Apple DTS responded to confirm that this <em>is</em> a bug, and that there is no workaround. For my case, I ended up simply forcing the disappearing cells not to <em>reach</em> the magic threshold that makes them disappear.</p>