CollectionViewLayout

open class CollectionViewLayout : NSObject

The CollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view. The job of a layout object is to determine the placement of cells, supplementary views inside the collection view’s bounds and to report that information to the collection view when asked. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.

  • The collection view this layout has been applied to

    Set when the layout is given to a collection view’s collectionViewLayout property

    Declaration

    Swift

    open internal(set) weak var collectionView: CollectionView? { get set }
  • The direction that the collection view should scroll

    Declaration

    Swift

    open var scrollDirection: CollectionViewScrollDirection { get }
  • The size that encapsulates all views within the collection view

    Note: Subclasses must override this method and use it to return the width and height of the collection view’s content. These values represent the width and height of all the content, not just the content that is currently visible. The collection view uses this information to configure its own content size to facilitate scrolling.

    Declaration

    Swift

    open var collectionViewContentSize: CGSize { get }
  • If supporting views should be pinned to the top of the view

    Declaration

    Swift

    open var pinHeadersToTop: Bool
  • Currently this is only called when the layout is applied to a collection view.

    Declaration

    Swift

    open func invalidate()
  • Asks the layout if it should be invalidated due to a bounds change on the collection view

    Declaration

    Swift

    open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool

    Parameters

    newBounds

    The new bounds of the collection view

    Return Value

    If the layout should be invalidated

  • Tells the layout object to update the current layout.

    Discussion

    Layout updates occur the first time the collection view presents its content and whenever the layout is invalidated explicitly or implicitly because of a change to the view. During each layout update, the collection view calls this method first to give your layout object a chance to prepare for the upcoming layout operation. The default implementation of this method does nothing. Subclasses can override it and use it to set up data structures or perform any initial computations needed to perform the layout later.

    Declaration

    Swift

    open func prepare()
  • All the index paths to be displayed by the collection view

    Becuase the layout likely needs to process all items in the data, setting this during prepare() can cut out the overhead of the collection view having to do so itself.

    Declaration

    Swift

    public var allIndexPaths: OrderedSet<IndexPath>
  • Returns the layout attributes for all views in a given rect

    Declaration

    Swift

    open func layoutAttributesForItems(in rect: CGRect) -> [CollectionViewLayoutAttributes]

    Parameters

    rect

    The rect in which to look for elements

  • Returns the layout attributes for an item at the given index path

    Important

    This must be overridden by subclasses

    Declaration

    Swift

    open func layoutAttributesForItem(at indexPath: IndexPath) -> CollectionViewLayoutAttributes?

    Parameters

    indexPath

    The index path of the item for which you want the attributes

  • Returns the layout attributes for the supplementary view of the given kind and the given index path

    Important

    This must be override by a subclass

    Declaration

    Swift

    open func layoutAttributesForSupplementaryView(ofKind kind: String, at indexPath: IndexPath) -> CollectionViewLayoutAttributes?

    Parameters

    elementKind

    The kind of the view

    indexPath

    The index path of the view

  • Returns the frame that encapsulates all the content in the section

    Declaration

    Swift

    open func rectForSection(_ section: Int) -> CGRect

    Parameters

    section

    The section to get the frame for

    Return Value

    The rect containing all the views

  • Returns the rect that encapsulates just the items of a section

    Declaration

    Swift

    open func contentRectForSection(_ section: Int) -> CGRect

    Parameters

    section

    The section to get the content frame for

    Return Value

    The rect containing all the items

  • Provides he layout a chance to adjust the frame to which the collection view should scroll to show an item

    The default implementation returns the value from layoutAttributesForItem(at:)

    Declaration

    Swift

    open func scrollRectForItem(at indexPath: IndexPath, atPosition: CollectionViewScrollPosition) -> CGRect?

    Parameters

    indexPath

    The item to scroll to

    atPosition

    The position at which to scroll the item to

  • Returns the index path for the next item in a given direction

    Declaration

    Swift

    open func indexPathForNextItem(moving direction: CollectionViewDirection, from currentIndexPath: IndexPath) -> IndexPath?

    Parameters

    direction

    The direction in which to look for the next items (up, down, left, right)

    currentIndexPath

    The current index path to seek from