Background Decoration View Overlapping Collection View Cells

I am encountering an issue with my UICollectionView layout where the background decoration view is overlapping the collection view cells. Below is the relevant code for my layout configuration:

let itemSize = NSCollectionLayoutSize(
    widthDimension: .absolute(60),
    heightDimension: .absolute(100)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)

let groupSize = NSCollectionLayoutSize(
    widthDimension: .fractionalWidth(1),
    heightDimension: .absolute(100)
)
let group = NSCollectionLayoutGroup.horizontal(
    layoutSize: groupSize,
    subitems: [item]
)

let section = NSCollectionLayoutSection(group: group)
section.decorationItems = [
    NSCollectionLayoutDecorationItem.background(elementKind: "customBackgroundElementKind")
]
return section

Problem: The background decoration view is appearing on top of the collection view cells, which results in the cells being obscured. This issue is specific to iOS 18 and does not occur on iOS 17 and below.

Request: Can anyone provide guidance or suggest a solution to ensure the decoration view does not overlap the collection view cells specifically on iOS 18?

Thank you!

I've checked iOS 18.1 beta as well. It's problem there as well.

@sunwukongTH

We had same problem in our project, this how we were able to solve it:


override func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
    guard elementKind == "customBackgroundElementKind" else { return }
    // Custom logic for setting background color
    let backgroundView = view as? BackgroundSection
    backgroundView?.setColor(.red)

    // actual workaround
    if #available(iOS 18.0, *) {
        backgroundView?.layer.zPosition = -5
    }
}

Are you able to reproduce the issue on iOS & iPadOS 18.1 Beta 4?

@DTS Engineer Although I have tested on iOS 18.1 beta 4, I am still encountering this issue. Could you kindly advise on what steps I should take next?

Accepted Answer

I've checked iOS 18.1 beta as well. It's problem there as well.

@sunwukongTH

We had same problem in our project, this how we were able to solve it:


override func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
    guard elementKind == "customBackgroundElementKind" else { return }
    // Custom logic for setting background color
    let backgroundView = view as? BackgroundSection
    backgroundView?.setColor(.red)

    // actual workaround
    if #available(iOS 18.0, *) {
        backgroundView?.layer.zPosition = -5
    }
}

DecorationItemView without blocking gestures: override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    let view = super.hitTest(point, with: event)

    if view == self {
        return nil
    }
    return view
}

And change layer.zPosition when willDisplaySupplementaryView:

func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
       //....
            if #available(iOS 18.0, *) {
                view.layer.zPosition = -5
            }
    }
Background Decoration View Overlapping Collection View Cells
 
 
Q