O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required