为了降低代码耦合性,通常自定义UICollectionViewFlowLayout。今天在自定义UICollectionViewFlowLayout时,想设置UICollectionView的footerView,但viewForSupplementaryElementOfKind不执行,改成系统的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;
}
网友评论