iOS Developer Library

Developer

UIKit Framework Reference UICollectionViewFlowLayout Class Reference

Options
Deployment Target:

On This Page
Language:

UICollectionViewFlowLayout

Inherits From


Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 6.0 and later

The UICollectionViewFlowLayout class is a concrete layout object that organizes items into a grid with optional header and footer views for each section. The items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row comprising as many cells as will fit. Cells can be the same sizes or different sizes.

A flow layout works with the collection view’s delegate object to determine the size of items, headers, and footers in each section and grid. That delegate object must conform to the UICollectionViewDelegateFlowLayout protocol. Use of the delegate allows you to adjust layout information dynamically. For example, you would need to use a delegate object to specify different sizes for items in the grid. If you do not provide a delegate, the flow layout uses the default values you set using the properties of this class.

Flow layouts lay out their content using a fixed distance in one direction and a scrollable distance in the other. For example, in a vertically scrolling grid, the width of the grid content is constrained to the width of the corresponding collection view while the height of the content adjusts dynamically to match the number of sections and items in the grid. The layout is configured to scroll vertically by default but you can configure the scrolling direction using the scrollDirection property.

Each section in a flow layout can have its own custom header and footer. To configure the header or footer for a view, you must configure the size of the header or footer to be non zero. You can do this by implementing the appropriate delegate methods or by assigning appropriate values to the headerReferenceSize and footerReferenceSize properties. If the header or footer size is 0, the corresponding view is not added to the collection view.

  • The scroll direction of the grid.

    Declaration

    Swift

    var scrollDirection: UICollectionViewScrollDirection

    Objective-C

    @property(nonatomic) UICollectionViewScrollDirection scrollDirection

    Discussion

    The grid layout scrolls along one axis only, either horizontally or vertically. For the non scrolling axis, the width of the collection view in that dimension serves as starting width of the content.

    The default value of this property is UICollectionViewScrollDirectionVertical.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The minimum spacing to use between lines of items in the grid.

    Declaration

    Swift

    var minimumLineSpacing: CGFloat

    Objective-C

    @property(nonatomic) CGFloat minimumLineSpacing

    Discussion

    If the delegate object does not implement the collectionView:layout:minimumLineSpacingForSectionAtIndex: method, the flow layout uses the value in this property to set the spacing between lines in a 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.

    The default value of this property is 10.0.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The minimum spacing to use between items in the same row.

    Declaration

    Swift

    var minimumInteritemSpacing: CGFloat

    Objective-C

    @property(nonatomic) CGFloat minimumInteritemSpacing

    Discussion

    If the delegate object does not implement the collectionView:layout:minimumInteritemSpacingForSectionAtIndex: method, the flow layout uses the value in this property to set the spacing between items in the same line.

    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.

    The default value of this property is 10.0.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • itemSize itemSize Property

    The default size to use for cells.

    Declaration

    Swift

    var itemSize: CGSize

    Objective-C

    @property(nonatomic) CGSize itemSize

    Discussion

    If the delegate does not implement the collectionView:layout:sizeForItemAtIndexPath: method, the flow layout uses the value in this property to set the size of each cell. This results in cells that all have the same size.

    The default size value is (50.0, 50.0).

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The estimated size of cells in the collection view.

    Declaration

    Swift

    var estimatedItemSize: CGSize

    Objective-C

    @property(nonatomic) CGSize estimatedItemSize

    Discussion

    Providing an estimated cell size can improve the performance of the collection view when the cells adjust their size dynamically. Specifying an estimate value lets the collection view defer some of the calculations needed to determine the actual size of its content. Specifically, cells that are not onscreen are assumed to be the estimated height.

    The default value of this property is CGSizeZero. Setting it to any other value causes the collection view to query each cell for its actual size using the cell’s preferredLayoutAttributesFittingAttributes: method. If all of your cells are the same height, use the itemSize property, instead of this property, to specify the cell size instead.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later

  • The margins used to lay out content in a section

    Declaration

    Swift

    var sectionInset: UIEdgeInsets

    Objective-C

    @property(nonatomic) UIEdgeInsets sectionInset

    Discussion

    If the delegate object does not implement the collectionView:layout:insetForSectionAtIndex: method, the flow layout uses the value in this property to set the margins for each section.

    Section insets reflect the spacing at the outer edges of the section. The margins affect the initial position of the header view, the minimum space on either side of each line of items, and the distance from the last line to the footer view. The margin insets do not affect the size of the header and footer views in the non scrolling direction.

    The default edge insets are all set to 0.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The default sizes to use for section headers.

    Declaration

    Swift

    var headerReferenceSize: CGSize

    Objective-C

    @property(nonatomic) CGSize headerReferenceSize

    Discussion

    If the delegate does not implement the collectionView:layout:referenceSizeForHeaderInSection: method, the flow layout object uses the default header sizes set in this property.

    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.

    The default size values are (0, 0).

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The default sizes to use for section footers.

    Declaration

    Swift

    var footerReferenceSize: CGSize

    Objective-C

    @property(nonatomic) CGSize footerReferenceSize

    Discussion

    If the delegate does not implement the collectionView:layout:referenceSizeForFooterInSection: method, the flow layout object uses the default footer sizes set for this property.

    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.

    The default size values are (0, 0).

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

    See Also

    sectionInset

  • Constants indicating the direction of scrolling for the layout.

    Declaration

    Swift

    enum UICollectionViewScrollDirection : Int { case Vertical case Horizontal }

    Objective-C

    enum { UICollectionViewScrollDirectionVertical, UICollectionViewScrollDirectionHorizontal }; typedef NSUInteger UICollectionViewScrollDirection;

    Constants

    • Vertical

      UICollectionViewScrollDirectionVertical

      The layout scrolls content vertically.

      Available in iOS 6.0 and later

    • Horizontal

      UICollectionViewScrollDirectionHorizontal

      The layout scrolls content horizontally.

      Available in iOS 6.0 and later

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • Constants that specify the types of supplementary views that can be presented using a flow layout.

    Declaration

    Swift

    let UICollectionElementKindSectionHeader: String let UICollectionElementKindSectionFooter: String

    Objective-C

    NSString *const UICollectionElementKindSectionHeader; NSString *const UICollectionElementKindSectionFooter;

    Constants

    • UICollectionElementKindSectionHeader

      UICollectionElementKindSectionHeader

      A supplementary view that identifies the header for a given section.

      Available in iOS 6.0 and later

    • UICollectionElementKindSectionFooter

      UICollectionElementKindSectionFooter

      A supplementary view that identifies the footer for a given section.

      Available in iOS 6.0 and later