為了降低代碼耦合性,通常自定義UICollectionViewFlowLayout。今天在自定義UICollectionViewFlowLayout時(shí),想設(shè)置UICollectionView的footerView,但viewForSupplementaryElementOfKind不執(zhí)行,改成系統(tǒng)的UICollectionViewFlowLayout就可以。
自定義UICollectionViewFlowLayout添加header 和footer的解決方法:
- (nullableNSArray<__kindofUICollectionViewLayoutAttributes*> *)layoutAttributesForElementsInRect:(CGRect)rect{
NSMutableArray*attributes = [[superlayoutAttributesForElementsInRect:rect]mutableCopy];
for(NSIntegeri =0; i <_numberOfCells; i++) {
NSIndexPath*indexPath = [NSIndexPathindexPathForItem:iinSection:0];
UICollectionViewLayoutAttributes*attribute = [selflayoutAttributesForItemAtIndexPath:indexPath];
[attributesaddObject:attribute];
}
for(UICollectionViewLayoutAttributes*attrsinattributes) {
if([attrsrepresentedElementKind] ==UICollectionElementKindSectionFooter) {
CGRectheaderRect = [attrsframe];
headerRect.size.height=100;
headerRect.size.width=SCREEN_SIZE.width;
[attrssetFrame:headerRect];
break;
}
}
returnattributes;
}