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