我开发了一个自定义 CollectionViewLayout 用于DecorationView
显示单元格后面的阴影。
但是,我只想将此装饰添加到某些单元格。是,但它可能包含嵌入在单元格内的UICollectionView
内容。如图所示,不应装饰带有嵌入的单元格:vertical
horizontal
UICollectionView
UICollectionView
这是我用来添加阴影的代码。没有提供如何检索单元格的类的UICollectionViewLayout
方法,因此它可以决定是否添加阴影:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let parent = super.layoutAttributesForElements(in: rect)
guard let attributes = parent, !attributes.isEmpty else {
return parent
}
let sections = attributes.map{$0.indexPath.section}
let unique = Array(Set(sections))
// Need to detect, which sections contain an embedded UICollectionView and exclude them from the UNIQUE set
let backgroundShadowAttributes: [UICollectionViewLayoutAttributes] = unique.compactMap{ section in
let indexPath = IndexPath(item: 0, section: section)
return self.layoutAttributesForDecorationView(ofKind: backgroundViewClass.reuseIdentifier(),
at: indexPath)
}
return attributes + backgroundShadowAttributes + separators
}
有没有办法有条件地指定应该装饰哪些视图?