iOS Developer Library

Developer

UIKit Framework Reference UICollectionViewDelegateFlowLayout Protocol Reference

Options
Deployment Target:

On This Page
Language:

UICollectionViewDelegateFlowLayout

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

Inheritance


Not Applicable

Import Statement


import UIKit @import UIKit;

Availability


Available in iOS 6.0 and later.
  • Asks the delegate for the size of the specified item’s cell.

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

    Objective-C

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

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets

    Objective-C

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

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat

    Objective-C

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

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat

    Objective-C

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

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize

    Objective-C

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

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize

    Objective-C

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

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.