title: Keeping the contentOffset in a UICollectionView while rotating Interface Orientation
body: <p>I'm trying to handle interface orientation changes in a UICollectionViewController. What I'm trying to achieve is, that I want to have the <em>same</em> contentOffset after an interface rotation. Meaning, that it should be changed corresponding to the ratio of the bounds change.</p>
  
  <p>Starting in portrait with a content offset of {<em>bounds.size.width</em> * 2, 0} …</p>
  
  <p><img src="https://i.stack.imgur.com/SQSRZ.png" alt="UICollectionView in portait"></p>
  
  <p>… should result to the content offset in landscape also with {<em>bounds.size.width</em> * 2, 0} (and vice versa).</p>
  
  <p><img src="https://i.stack.imgur.com/GmrMq.png" alt="UICollectionView in landscape"></p>
  
  <p>Calculating the new offset is not the problem, but don't know, where (or when) to set it, to get a smooth animation. What I'm doing so fare is invalidating the layout in <code>willRotateToInterfaceOrientation:duration:</code> and resetting the content offset in <code>didRotateFromInterfaceOrientation:</code>:</p>
  
  <pre><code>- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                  duration:(NSTimeInterval)duration;
  {
      self.scrollPositionBeforeRotation = CGPointMake(self.collectionView.contentOffset.x / self.collectionView.contentSize.width,
                                                      self.collectionView.contentOffset.y / self.collectionView.contentSize.height);
      [self.collectionView.collectionViewLayout invalidateLayout];
  }
  
  - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
  {
      CGPoint newContentOffset = CGPointMake(self.scrollPositionBeforeRotation.x * self.collectionView.contentSize.width,
                                             self.scrollPositionBeforeRotation.y * self.collectionView.contentSize.height);
      [self.collectionView newContentOffset animated:YES];
  }
  </code></pre>
  
  <p>This changes the content offset after the rotation.</p>
  
  <p>How can I set it during the rotation? I tried to set the new content offset in <code>willAnimateRotationToInterfaceOrientation:duration:</code> but this results into a very strange behavior.</p>
  
  <p>An example can be found in my Project on <a href="https://github.com/anagromataf/TKViewControllerCollectionController/blob/develop/TKViewControllerCollectionController/TKViewControllerCollectionController.m#L86" rel="noreferrer">GitHub</a>.</p>