UICollectionView 自定義section背景

需求:

在collectionView的每個(gè)section頭部加一個(gè)漸變從section頭部的白色變成灰色背景色
41A5479B-16C4-4533-965F-E2086C2C9E6F.png

解決方案:

繼承UICollectionViewFlowLayout寫一個(gè)自定義的layout
重寫init方法, 初始化layout, 注冊(cè)XXXDecorationView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.itemSize = [XXXItemViewCell getCellSize];
        self.sectionInset = UIEdgeInsetsMake(0,12,9,12);
        self.minimumInteritemSpacing =9;
        self.headerReferenceSize = CGSizeMake(FULL_WIDTH,47.5);
        self.footerReferenceSize = CGSizeMake(FULL_WIDTH, 0);
        self.minimumLineSpacing = 9;
        [self registerClass:[XXXDecorationView class] forDecorationViewOfKind:@"XXXDecorationView"];//注冊(cè)Decoration View
    }
    return self;
}

重寫準(zhǔn)備布局方法, 構(gòu)建DecorationView不同indexPath的frame

- (void)prepareLayout{
    
    [super prepareLayout];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    NSInteger sectionCount = self.collectionView.numberOfSections;
    if (sectionCount <= 0) {
        return;
    }
    
    CGFloat y = 0; //DecorationView的y坐標(biāo)
    CGFloat availableWidth = self.collectionViewContentSize.width - (self.sectionInset.left + self.sectionInset.right); //section寬
    int itemsAcross = floorf((availableWidth + self.minimumInteritemSpacing) / (self.itemSize.width + self.minimumInteritemSpacing)); //每行幾個(gè)item
    
    for (int section = 0; section < sectionCount; section++)
    {
        y += self.headerReferenceSize.height;
        y += self.sectionInset.top;
        
        dictionary[[NSIndexPath indexPathForItem:0 inSection:section]] = [NSValue valueWithCGRect:CGRectMake(0, y, self.collectionViewContentSize.width, 92)];
        
        NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
        int rows = ceilf(itemCount/(float)itemsAcross); //每個(gè)section的item行數(shù)
        for (int row = 0; row < rows; row++)
        {
            y += self.itemSize.height;
            
            if (row < rows - 1) {
                y += self.minimumLineSpacing;
            }
        }
        y += self.sectionInset.bottom;
        y += self.footerReferenceSize.height;
    }
    
    self.shelfRects = [NSDictionary dictionaryWithDictionary:dictionary];
}

重寫Decoration View的布局不傅。

- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath *)indexPath{
    id shelfRect = self.shelfRects[indexPath];
    if (!shelfRect) {
        return nil;
    }
    
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"XXXDecorationView" withIndexPath:indexPath];
    attributes.frame = [shelfRect CGRectValue];
    attributes.zIndex = -1;
    
    return attributes;
}

并把Decoration View的布局加入可見區(qū)域布局。

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    //把Decoration View的布局加入可見區(qū)域布局。
    NSMutableArray *newArray = [attributes mutableCopy];
    
    [self.shelfRects enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if (CGRectIntersectsRect([obj CGRectValue], rect))
        {
            UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"XXXDecorationView" withIndexPath:key];
            attributes.frame = [obj CGRectValue];
            attributes.zIndex = -1;
            [newArray addObject:attributes];
        }
    }];
    
    attributes = [NSArray arrayWithArray:newArray];
    
    return attributes;
}

XXXDecorationView需繼承于UICollectionReusableView。
效果圖

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市扑眉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖眯亦,帶你破解...
    沈念sama閱讀 212,454評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異般码,居然都是意外死亡妻率,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門板祝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宫静,“玉大人,你說我怎么就攤上這事」吕铮” “怎么了伏伯?”我有些...
    開封第一講書人閱讀 157,921評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)捌袜。 經(jīng)常有香客問我说搅,道長(zhǎng),這世上最難降的妖魔是什么琢蛤? 我笑而不...
    開封第一講書人閱讀 56,648評(píng)論 1 284
  • 正文 為了忘掉前任蜓堕,我火速辦了婚禮,結(jié)果婚禮上博其,老公的妹妹穿的比我還像新娘套才。我一直安慰自己,他們只是感情好慕淡,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評(píng)論 6 386
  • 文/花漫 我一把揭開白布背伴。 她就那樣靜靜地躺著,像睡著了一般峰髓。 火紅的嫁衣襯著肌膚如雪傻寂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,950評(píng)論 1 291
  • 那天携兵,我揣著相機(jī)與錄音疾掰,去河邊找鬼。 笑死徐紧,一個(gè)胖子當(dāng)著我的面吹牛静檬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播并级,決...
    沈念sama閱讀 39,090評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼拂檩,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了嘲碧?” 一聲冷哼從身側(cè)響起稻励,我...
    開封第一講書人閱讀 37,817評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎愈涩,沒想到半個(gè)月后望抽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,275評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡履婉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評(píng)論 2 327
  • 正文 我和宋清朗相戀三年糠聪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谐鼎。...
    茶點(diǎn)故事閱讀 38,724評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出狸棍,到底是詐尸還是另有隱情身害,我是刑警寧澤,帶...
    沈念sama閱讀 34,409評(píng)論 4 333
  • 正文 年R本政府宣布草戈,位于F島的核電站塌鸯,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏唐片。R本人自食惡果不足惜丙猬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評(píng)論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望费韭。 院中可真熱鬧茧球,春花似錦、人聲如沸星持。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)督暂。三九已至揪垄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間逻翁,已是汗流浹背饥努。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留八回,地道東北人酷愧。 一個(gè)月前我還...
    沈念sama閱讀 46,503評(píng)論 2 361
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像辽社,于是被迫代替她去往敵國(guó)和親伟墙。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容