UICollectionView 修改组背景颜色

.h

HTCollectionViewLayout : UICollectionViewFlowLayout

HTCollectionViewLayoutAttributes:UICollectionViewLayoutAttributes

HTCollectionReusableView :UICollectionReusableView

@protocol HTCollectionViewLayoutDelegate <UICollectionViewDelegateFlowLayout>

//  Section 背景颜色

@optional

-(UIColor *)collectionview:(UICollectionView *)collectionView colorForSection:(NSInteger)section;

@end


.m

- (void)prepareLayout{

    [super prepareLayout];

    

    

    [self registerClass:[HTCollectionReusableView class] forDecorationViewOfKind:@"HTCollectionReusableView"];

//    获取组数

    NSInteger sections = [self.collectionView numberOfSections];

    

//    获取代理对象

    __strong id<HTCollectionViewLayoutDelegate> delegate  = self.collectionView.delegate;

//    是否实现代理方法

    if ([delegate respondsToSelector:@selector(collectionview:colorForSection:)]) {

        [self.decorationViewAttrs removeAllObjects];

        

//        遍历组

        for(NSInteger section = 0;section<sections;section ++){

//            获取Item数

            NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];

            if(numberOfItems >0){

//                获取第一个item Attributes

                UICollectionViewLayoutAttributes *fristAttributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

                

                UICollectionViewLayoutAttributes *lastAttrbutes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:(numberOfItems - 1) inSection:section]];

                

//                获取最小包含frame

                CGRect sectionFrame = CGRectUnion(fristAttributes.frame, lastAttrbutes.frame);

                

                if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {

                    sectionFrame.size.height = self.collectionView.frame.size.height;

                }else{

                    sectionFrame.size.width = self.collectionView.frame.size.width;

                }

                

                HTCollectionViewLayoutAttributes *attr = [HTCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"HTCollectionReusableView" withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];

                attr.frame = sectionFrame;

                attr.zIndex = -1;

                attr.backgroudColor = [delegate collectionview:self.collectionView colorForSection:section];

                

                [self.decorationViewAttrs addObject:attr];

            }

            

        }

        

        

    }else{

        return ;

    }

    

}




-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

    NSMutableArray *attrs = [NSMutableArray array];

    [attrs addObjectsFromArray:[[super layoutAttributesForElementsInRect:rect] mutableCopy]];

    for(UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs){

        if(CGRectIntersectsRect(rect, attr.frame)){

            [attrs addObject:attr];

        }

    }

    return attrs;

}


@implementation HTCollectionReusableView

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    [super applyLayoutAttributes:layoutAttributes];

    HTCollectionViewLayoutAttributes *attr = (HTCollectionViewLayoutAttributes *)layoutAttributes;

    self.backgroundColor = attr.backgroudColor;

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值