为UICollectionView的section添加backg
2020-10-22 本文已影响0人
init123
主要思想为使用DecorationView
1.定义DecorationView
class SectionBackgroundView : UICollectionReusableView {
//xxxxx
}
2.派生一个UICollectionViewLayout
prepare()方法中声明DecorationView
layout.register(SectionBackgroundView.self, forDecorationViewOfKind: "SectionBg")
3.创建DecorationView对应的UICollectionViewLayoutAttributes
let attri = UICollectionViewLayoutAttributes(forDecorationViewOfKind: "SectionBg", with: IndexPath(item: 0, section: section))
attri.zIndex = -1;
attri.frame = frame; //计算frame
4.在layoutAttributesForElements中使用