UICollectionViewDelegateFlowLayout Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 6.0 and later.
Declared in
UICollectionViewFlowLayout.h

Overview

The UICollectionViewDelegateFlowLayout protocol defines methods that let you coordinate with a UICollectionViewFlowLayout object to implement a grid-based layout. The methods of this protocol define the size of items and the spacing between items in the grid.

All of the methods in this protocol are optional. If you do not implement a particular method, the flow layout delegate uses values in its own properties for the appropriate spacing information.

The flow layout object expects the collection view’s delegate object to adopt this protocol. Therefore, implement this protocol on object assigned to your collection view’s delegate property.

Tasks

Getting the Size of Items

Getting the Section Spacing

Getting the Header and Footer Sizes

Instance Methods

collectionView:layout:insetForSectionAtIndex:

Asks the delegate for the margins to apply to content in the specified section.

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
Parameters
collectionView

The collection view object displaying the flow layout.

collectionViewLayout

The layout object requesting the information.

section

The index number of the section whose insets are needed.

Return Value

The margins to apply to items in the section.

Discussion

If you do not implement this method, the flow layout uses the value in its sectionInset property to set the margins instead. Your implementation of this method can return a fixed set of margin sizes or return different margin sizes for each section.

Section insets are margins applied only to the items in the section. They represent the distance between the header view and the first line of items and between the last line of items and the footer view. They also indicate they spacing on either side of a single line of items. They do not affect the size of the headers or footers themselves.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewFlowLayout.h

collectionView:layout:minimumInteritemSpacingForSectionAtIndex:

Asks the delegate for the spacing between successive items in the rows or columns of a section.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
Parameters
collectionView

The collection view object displaying the flow layout.

collectionViewLayout

The layout object requesting the information.

section

The index number of the section whose inter-item spacing is needed.

Return Value

The minimum space (measured in points) to apply between successive items in the lines of a section.

Discussion

If you do not implement this method, the flow layout uses the value in its minimumInteritemSpacing property to set the space between items instead. Your implementation of this method can return a fixed value or return different spacing values for each section.

For a vertically scrolling grid, this value represents the minimum spacing between items in the same row. For a horizontally scrolling grid, this value represents the minimum spacing between items in the same column. This spacing is used to compute how many items can fit in a single line, but after the number of items is determined, the actual spacing may possibly be adjusted upward.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewFlowLayout.h

collectionView:layout:minimumLineSpacingForSectionAtIndex:

Asks the delegate for the spacing between successive rows or columns of a section.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
Parameters
collectionView

The collection view object displaying the flow layout.

collectionViewLayout

The layout object requesting the information.

section

The index number of the section whose line spacing is needed.

Return Value

The minimum space (measured in points) to apply between successive lines in a section.

Discussion

If you do not implement this method, the flow layout uses the value in its minimumLineSpacing property to set the space between lines instead. Your implementation of this method can return a fixed value or return different spacing values for each section.

For a vertically scrolling grid, this value represents the minimum spacing between successive rows. For a horizontally scrolling grid, this value represents the minimum spacing between successive columns. This spacing is not applied to the space between the header and the first line or between the last line and the footer.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewFlowLayout.h

collectionView:layout:referenceSizeForFooterInSection:

Asks the delegate for the size of the footer view in the specified section.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
Parameters
collectionView

The collection view object displaying the flow layout.

collectionViewLayout

The layout object requesting the information.

section

The index of the section whose footer size is being requested.

Return Value

The size of the footer. If you return a value of size (0, 0), no footer is added.

Discussion

If you do not implement this method, the flow layout uses the value in its footerReferenceSize property to set the size of the footer.

During layout, only the size that corresponds to the appropriate scrolling direction is used. For example, for the vertical scrolling direction, the layout object uses the height value specified by this property. (In that instance, the width of the footer would be set to the width of the collection view.) If the size in the appropriate scrolling dimension is 0, no footer is added.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewFlowLayout.h

collectionView:layout:referenceSizeForHeaderInSection:

Asks the delegate for the size of the header view in the specified section.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
Parameters
collectionView

The collection view object displaying the flow layout.

collectionViewLayout

The layout object requesting the information.

section

The index of the section whose header size is being requested.

Return Value

The size of the header. If you return a value of size (0, 0), no header is added.

Discussion

If you do not implement this method, the flow layout uses the value in its headerReferenceSize property to set the size of the header.

During layout, only the size that corresponds to the appropriate scrolling direction is used. For example, for the vertical scrolling direction, the layout object uses the height value returned by your method. (In that instance, the width of the header would be set to the width of the collection view.) If the size in the appropriate scrolling dimension is 0, no header is added.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewFlowLayout.h

collectionView:layout:sizeForItemAtIndexPath:

Asks the delegate for the size of the specified item’s cell.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
Parameters
collectionView

The collection view object displaying the flow layout.

collectionViewLayout

The layout object requesting the information.

indexPath

The index path of the item.

Return Value

The width and height of the specified item. Both values must be greater than 0.

Discussion

If you do not implement this method, the flow layout uses the values in its itemSize property to set the size of items instead. Your implementation of this method can return a fixed set of sizes or dynamically adjust the sizes based on the cell’s content.

The flow layout does not crop a cell’s bounds to make it fit into the grid. Therefore, the values you return must allow for the item to be displayed fully in the collection view. For example, in a vertically scrolling grid, the width of a single item must not exceed the width of the collection view view (minus any section insets) itself. However, in the scrolling direction, items can be larger than the collection view because the remaining content can always be scrolled into view.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewFlowLayout.h