#import
/// 扩展section的背景色
@protocol ULBCollectionViewDelegateFlowLayout
- (UIColor*)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutcolorForSectionAtIndex:(NSInteger)section;
@end
@interface ULBCollectionViewFlowLayout : UICollectionViewFlowLayout
@end
////////
#import "ULBCollectionViewFlowLayout.h"
static NSString *const ULBCollectionViewSectionColor = @"com.ulb.ULBCollectionElementKindSectionColor";
@interface ULBCollectionViewLayoutAttributes :UICollectionViewLayoutAttributes
// 背景色
@property (nonatomic, strong) UIColor *backgroudColor;
@end
@implementation ULBCollectionViewLayoutAttributes
@end
@interface ULBCollectionReusableView : UICollectionReusableView
@end
@implementation ULBCollectionReusableView
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
[superapplyLayoutAttributes:layoutAttributes];
ULBCollectionViewLayoutAttributes *attr = (ULBCollectionViewLayoutAttributes *)layoutAttributes;
self.backgroundColor = attr.backgroudColor;
}
@end
@interface ULBCollectionViewFlowLayout ()
@property (nonatomic, strong) UIColor *sectonColor;
@property (nonatomic, strong) NSMutableArray
@end
@implementation ULBCollectionViewFlowLayout
- (void)prepareLayout{
[super prepareLayout];
NSInteger sections = [self.collectionView numberOfSections];
id
if([delegaterespondsToSelector:@selector(collectionView:layout:colorForSectionAtIndex:)]) {
}else{
return;
}
//1.初始化
[self registerClass:[ULBCollectionReusableView class] forDecorationViewOfKind:ULBCollectionViewSectionColor];
[self.decorationViewAttrs removeAllObjects];
for(NSIntegersection =0; section < sections; section++) {
NSIntegernumberOfItems = [self.collectionViewnumberOfItemsInSection:section];
if(numberOfItems >0) {
UICollectionViewLayoutAttributes *firstAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
UICollectionViewLayoutAttributes*lastAttr = [selflayoutAttributesForItemAtIndexPath:[NSIndexPathindexPathForRow:(numberOfItems -1)inSection:section]];
UIEdgeInsetssectionInset =self.sectionInset;
if([delegaterespondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
UIEdgeInsets inset = [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
if(!UIEdgeInsetsEqualToEdgeInsets(inset, sectionInset)) {
sectionInset = inset;
}
}
CGRectsectionFrame =CGRectUnion(firstAttr.frame, lastAttr.frame);
sectionFrame.origin.x-= sectionInset.left;
sectionFrame.origin.y-= sectionInset.top;
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
sectionFrame.size.width+= sectionInset.left+ sectionInset.right;
sectionFrame.size.height = self.collectionView.frame.size.height;
}else{
sectionFrame.size.width=self.collectionView.frame.size.width;
sectionFrame.size.height+= sectionInset.top+ sectionInset.bottom;
}
//2. 定义
ULBCollectionViewLayoutAttributes *attr = [ULBCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:ULBCollectionViewSectionColor withIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
attr.frame= sectionFrame;
attr.zIndex= -1;
attr.backgroudColor = [delegate collectionView:self.collectionView layout:self colorForSectionAtIndex:section];
[self.decorationViewAttrsaddObject:attr];
}else{
continue;
}
}
}
- (NSArray
NSMutableArray * attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {
if(CGRectIntersectsRect(rect, attr.frame)) {
[attrsaddObject:attr];
}
}
return[attrscopy];
}
- (NSMutableArray
if (!_decorationViewAttrs) {
_decorationViewAttrs = [NSMutableArray array];
}
return _decorationViewAttrs;
}
@end