Environment:
UICollectionView that looks like a UITableView
Custom subclass of UICollectionViewFlowLayout to define frame DecorationView
Self-sized cells are included.
Expected Behavior:
A DecorationView that should be placed as the background for each section of the UICollectionView

Observed Behavior:
DecorationView collapses to an arbitrary size:

It seems that UICollectionView trying to calculate the automatic size for DecorationView . If I turn off Self-Sizing cells, the decoration view will be placed exactly in the expected location.
Is it possible to disable self-esteem for DecorationView ?
In my subclass of UICollectionViewFlowLayout I just take the first and last cells in the section and stretch the background to fill the space under them. The problem is that the UICollectionView does not take into account the size calculated there:
override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { guard let collectionView = collectionView else { return nil } let section = indexPath.section let attrs = UICollectionViewLayoutAttributes(forDecorationViewOfKind: backgroundViewClass.reuseIdentifier(), with: indexPath) let numberOfItems = collectionView.numberOfItems(inSection: section) let lastIndex = numberOfItems - 1 guard let firstItemAttributes = layoutAttributesForItem(at: IndexPath(indexes: [section, 0])), let lastItemAttributes = layoutAttributesForItem(at: IndexPath(indexes: [section, lastIndex])) else { return nil } let startFrame = firstItemAttributes.frame let endFrame = lastItemAttributes.frame let origin = startFrame.origin let size = CGSize(width: startFrame.width, height: -startFrame.minY + endFrame.maxY) let frame = CGRect(origin: origin, size: size) attrs.frame = frame attrs.zIndex = -1 return attrs }
ios autolayout swift uicollectionview
Richard Topchiy
source share