An object used to add a background to a section of a collection view.


class NSCollectionLayoutDecorationItem : NSCollectionLayoutItem


Each type of decoration item must have a unique element kind. Consider tracking these strings together in a way that makes it straightforward to identify each element.

struct ElementKind {
    static let badge = "badge-element-kind"
    static let background = "background-element-kind"
    static let sectionHeader = "section-header-element-kind"
    static let sectionFooter = "section-footer-element-kind"
    static let layoutHeader = "layout-header-element-kind"
    static let layoutFooter = "layout-footer-element-kind"

Add a background to a section by setting that section's decorationItems property:

let sectionBackground = NSCollectionLayoutDecorationItem.background(
        elementKind: ElementKind.background)

section.decorationItems = [sectionBackground]

let layout = UICollectionViewCompositionalLayout(section: section)
    forDecorationViewOfKind: ElementKind.background)
return layout


Creating a Background

class func background(elementKind: String) -> Self

Creates a section background with a string to identify the element kind.

Getting the Element Kind

var elementKind: String

A string that identifies the type of decoration item.

Specifying Stacking Order

var zIndex: Int

The vertical stacking order of the decoration item in relation to other items in the section.


See Also


class NSCollectionLayoutAnchor

An object that defines how to attach a supplementary item to an item in a collection view.

class NSCollectionLayoutSupplementaryItem

An object used to add an extra visual decoration, such as a badge or a frame, to an item in a collection view.

class NSCollectionLayoutBoundarySupplementaryItem

An object used to add headers or footers to a collection view.