5.3. Providing a Flow Layout to a Collection View

Problem

You want to provide a grid-like layout to your collection view so that your content can render in a way similar to that shown in Figure 5-1.

Solution

Create an instance of the UICollectionViewFlowLayout class, instantiate your collection view controller using the initWithCollectionViewLayout: designated initializer of the UICollectionViewController class, and pass your layout object to this method.

Discussion

A flow layout can easily be instantiated, but before it can be passed to a collection view, it has to be configured. Here we are going to discuss the various properties that you can tweak on an instance of the UICollectionViewFlowLayout class and how they can affect the rendering of your collection view cells:

minimumLineSpacing

A floating point value that dictates to the flow layout the minimum number of points it has to reserve between each row. The layout object may decide to allocate more space in order to make the layout look good, but it must not allocate less. If your collection view is too small for the items to fit into it, your items will be clipped, just like any other view in the iOS SDK.

minimumInteritemSpacing

A floating point value to indicate the minimum number of points that the layout should reserve between cells on the same row. Again, this is the minimum number of points, and the layout, depending on the size of the collection view, may decide to increase this number.

itemSize

A CGSize that specifies the size ...

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.