UICollectionView 頭部的固定

對于 UICollectionView 的使用汹买,當(dāng)我們遇到使用頭部的時候,會發(fā)現(xiàn)頭部會一直跟隨下面的 cell一直移動的,然而當(dāng)我們需要怎樣固定呢拷姿?iOS 9之前,是需要通過改變 UICollectionViewFlowLayout 中的某些值來實現(xiàn)的旱函。

經(jīng)過NickLin的提醒响巢,iOS 9之后直接用這兩個屬性,設(shè)為YES就OK啦棒妨。

// Set these properties to YES to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to UITableView).
@property (nonatomic) BOOL sectionHeadersPinToVisibleBounds NS_AVAILABLE_IOS(9_0);
@property (nonatomic) BOOL sectionFootersPinToVisibleBounds NS_AVAILABLE_IOS(9_0);

下面是通過改變UICollectionViewFlowLayout 的某些值實現(xiàn)的踪古。

#import <UIKit/UIKit.h>

@interface PQCollectionHeaderLayout : UICollectionViewFlowLayout

@end
#import "PQCollectionHeaderLayout.h"

@implementation PQCollectionHeaderLayout

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
    for (NSUInteger idx=0; idx<[answer count]; idx++) {
        UICollectionViewLayoutAttributes *layoutAttributes = answer[idx];
        
        if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
            [missingSections addIndex:layoutAttributes.indexPath.section];  // remember that we need to layout header for this section
        }
        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
            [answer removeObjectAtIndex:idx];  // remove layout of header done by our super, we will do it right later
            idx--;
        }
    }
    
    // layout all headers needed for the rect using self code
    [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
        UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
        if (layoutAttributes != nil) {
            [answer addObject:layoutAttributes];
        }
    }];
    
    return answer;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionView * const cv = self.collectionView;
        CGPoint const contentOffset = cv.contentOffset;
        CGPoint nextHeaderOrigin = CGPointMake(INFINITY, INFINITY);
        
        if (indexPath.section+1 < [cv numberOfSections]) {
            UICollectionViewLayoutAttributes *nextHeaderAttributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section+1]];
            nextHeaderOrigin = nextHeaderAttributes.frame.origin;
        }
        
        CGRect frame = attributes.frame;
        if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
            frame.origin.y = MIN(MAX(contentOffset.y, frame.origin.y), nextHeaderOrigin.y - CGRectGetHeight(frame));
        }
        else { // UICollectionViewScrollDirectionHorizontal
            frame.origin.x = MIN(MAX(contentOffset.x, frame.origin.x), nextHeaderOrigin.x - CGRectGetWidth(frame));
        }
        attributes.zIndex = 1024;
        attributes.frame = frame;
    }
    return attributes;
}

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
    return attributes;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
    return attributes;
}


- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {
    return YES;
}

然后我們就可以直接讓UICollectionView 直接使用上述的UICollectionViewFlowLayout就可以達(dá)到效果啦。

摘錄:http://stackoverflow.com/questions/15233180/uicollectionview-with-a-sticky-header

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末券腔,一起剝皮案震驚了整個濱河市伏穆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌颅眶,老刑警劉巖蜈出,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異涛酗,居然都是意外死亡铡原,警方通過查閱死者的電腦和手機(jī)偷厦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來燕刻,“玉大人只泼,你說我怎么就攤上這事÷严矗” “怎么了请唱?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長过蹂。 經(jīng)常有香客問我十绑,道長,這世上最難降的妖魔是什么酷勺? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任本橙,我火速辦了婚禮,結(jié)果婚禮上脆诉,老公的妹妹穿的比我還像新娘甚亭。我一直安慰自己,他們只是感情好击胜,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布亏狰。 她就那樣靜靜地躺著,像睡著了一般偶摔。 火紅的嫁衣襯著肌膚如雪暇唾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天啰挪,我揣著相機(jī)與錄音信不,去河邊找鬼。 笑死亡呵,一個胖子當(dāng)著我的面吹牛抽活,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锰什,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼下硕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了汁胆?” 一聲冷哼從身側(cè)響起梭姓,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嫩码,沒想到半個月后誉尖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡铸题,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年铡恕,在試婚紗的時候發(fā)現(xiàn)自己被綠了琢感。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出专酗,到底是詐尸還是另有隱情,我是刑警寧澤坷牛,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏苛蒲。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一绿满、第九天 我趴在偏房一處隱蔽的房頂上張望撤防。 院中可真熱鬧,春花似錦棒口、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至厂抖,卻和暖如春茎毁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背忱辅。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工七蜘, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人墙懂。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓橡卤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親损搬。 傳聞我的和親對象是個殘疾皇子碧库,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件巧勤、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,121評論 4 61
  • 時間:2015年5月31日 機(jī)型:mi4 版本:美團(tuán)外賣 V3.4.3 百度外賣 V1.8.1 從 產(chǎn)品階段嵌灰、歷...
    今夏Summer閱讀 11,564評論 0 9
  • 親愛的愛麗絲 世事如書 或?qū)⑾В驅(qū)⒂篮?也許你不會來 也許我不會走 聽聽草木俱朽的枯榮 嗅嗅萬物重生的喜悅 先...
    唐小欣不小心丟了糖閱讀 389評論 2 1
  • 很久沒有好好寫了颅悉,覺得自己快要荒廢了沽瞭。 經(jīng)常看妹妹寫的那些我們共同摻雜的童年過往剩瓶,總是忍不住的心酸驹溃。更...
    柳絮輕舞閱讀 235評論 0 0