Thursday, December 11, 2014

Possible Problems with UICollectionView Animations not working when rotating.

If you are using UICollectionView and would like it to animate moving the cells around when rotating the device but it is not working, there might be an important reason. The animation methods (-(UICollectionViewLayoutAttributes*) initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *) itemIndexPath and -(UICollectionViewLayoutAttributes*) finalLayoutAttributesForDisappearingItemAtIndexPath: (NSIndexPath *) itemIndexPath ) will only get calls if you are using autolayout. If you are using frames to move things around, it won't call the methods in questions when rotating. Turn on the autolayout by calling (    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) on the collection view and it should start calling the methods correctly when rotating. You might also have to set this for the parents as well.

The down side is, if you are changing the size of the cells as well, you might have to redo them using contraints instead of frames.

If you are interesting in animations at all with UICollectionViews, I suggest you go here