4
votes

I have a UICollectionView with the frame of

[UIScreen mainScreen].bounds

and these attributes:

_collectionView.pagingEnabled = YES;
_collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

A cell has also the size of the collection view. When I rotate, the contentOffset of the collection view does not fit to the new orientation. It has still the same offset as before the rotation.

To fix this, I changed the contentOffset manually in the didRotate method.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    _collectionView.contentOffset = CGPointMake(_newContentOffsetX, _collectionView.contentOffset.y);
}

This works, but it looks terrible. I also tried to scroll to the current IndexPath, but it hast the same ugly behaviour:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [_collectionView scrollToItemAtIndexPath:_currentIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}

I need a clean transition and behaviour of updating the collection view's content offset when rotating the device.

2

2 Answers

3
votes

Its better To use the

willAnimateRotationToInterfaceOrientation:duration:

When this method is called the bound of your view controllers view are already updated to the current device orientation. It seems that this method gets called inside of the rotation animation block. This means all the positions you will set inside of willAnimateRotationToInterfaceOrientation:duration: are animated.

This helps me a lot and I use it to update my view Controllers view content insets on rotation. Works like a charm! :-)

-1
votes

Instead of using didRotateFromInterfaceOrientation, try updating contentOffset in willRotateToInterfaceOrientation. You'll need to make sure to compensate for using the other width/height dimension that you'll be rotating to since it won't have done the rotation yet.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    _collectionView.contentOffset = CGPointMake(_newContentOffsetX, _collectionView.contentOffset.y);
}