UICollectionView Example with UICollectionViewFlowLayout

I’m waiting for some friends to come into town tonight so I thought I might throw together a very basic UICollectionViewFlowLayout example.

It demonstrates proper use of:

  • UICollectionViewFlowLayout properties and basic subclassing.
  • How to properly implement UICollectionViewCell subclasses.
  • How to change layouts (with animation).
  • How to set up a collection view without a .xib or Storyboard.

Important things to note:

  • Always invalidate a layout before setting giving it to a collection view to use. This is really important if your app supports more than one orientation.
  • Always reset UICollectionViewCells to neutral when you override prepareForReuse.
  • Always use setCollectionViewLayout:animated: to change a layout with animation – never set the property in an animation block and assume it will work.

These are just a few of the gems I’ve picked up while writing a short book on UICollectionView. I’ll let you know when it’s available for sale.

B62F5B2D6C2F49469A7C0EC8F5FE3C6A

AB89F722D59045A080987447EFE6BFE6

I’ll probably add more layouts to it other than small and large ones. If anyone has anything they want to add or suggestions, let me know.


Please submit typo corrections on GitHub