UICollectionView: 添加類似UITableView的tableHeaderView的頁眉屬性

在iOS開發(fā)中, 我們會(huì)經(jīng)常使用到UICollectionView和UITableView, 但是在使用過程中總會(huì)出現(xiàn)各種各樣的需求
由于UITableView擁有tableHeaderView, 可以在每個(gè)分區(qū)的header之外, 還可以設(shè)置頁眉, 一般我們的首頁輪播圖就是放在這個(gè)位置
UICollectionView沒有類似tableHeaderView的屬性, 只能設(shè)置每個(gè)分區(qū)的header, 所以我們想要給UICollectionView添加頁眉, 就需要自己設(shè)置

UICollectionView有一個(gè)屬性: contentInset, 可以設(shè)置UICollectionView的內(nèi)邊距, 而不改變contentSize的大小
我們在設(shè)置頁眉的時(shí)候都會(huì)先改變contentInset的top值, 讓內(nèi)容向下移動(dòng)一段距離, 然后在這個(gè)空白的位置添加我們需要的頁眉

下面是我自己寫的一個(gè)分類, 添加了一個(gè)屬性, 這個(gè)屬性類似于UITableView的tableHeaderView
.h文件

#import <UIKit/UIKit.h>
@interface UICollectionView (LTExtension)
/** 頁眉視圖 */
@property (nonatomic) UIView *lt_collectionHeaderView;
/** 頁腳視圖 */
@property (nonatomic) UIView *lt_collectionFooterView;
/** 頁眉向上偏移量 */
@property (nonatomic, assign) CGFloat lt_collectionHeaderViewOffset;
/** 頁腳向下偏移量 */
@property (nonatomic, assign) CGFloat lt_collectionFooterViewOffset;
@end

.m文件

#import "UICollectionView+LTExtension.h"
#import <objc/runtime.h>

static const NSString *kCollectionHeaderViewKey = @"lt_collectionHeaderView";
static const NSString *kCollectionFooterViewKey = @"lt_collectionFooterView";

static const NSString *kCollectionHeaderViewOffsetKey = @"lt_collectionHeaderViewOffset";
static const NSString *kCollectionFooterViewOffsetKey = @"lt_collectionFooterViewOffset";

@implementation UICollectionView (LTExtension)

#pragma mark - < 頁眉 >

/**
 設(shè)置頁眉 - set方法
 */
- (void)setLt_collectionHeaderView:(UIView *)lt_collectionHeaderView
{
    UIView *headerView = self.lt_collectionHeaderView;
    if (headerView != lt_collectionHeaderView) {
        // 獲取內(nèi)邊距
        UIEdgeInsets contentInset = self.contentInset;
        
        // 移除之前的lt_collectionHeaderView
        contentInset.top -= headerView.height;
        [headerView removeFromSuperview];
        
        // 設(shè)置內(nèi)邊距
        contentInset.top += lt_collectionHeaderView.height;
        self.contentInset = contentInset;
        
        // 添加lt_collectionHeaderView
        [self addSubview:lt_collectionHeaderView];
        
        objc_setAssociatedObject(self, &(kCollectionHeaderViewKey), lt_collectionHeaderView, OBJC_ASSOCIATION_ASSIGN);
    }
}

/**
 獲取頁眉 - get方法
 */
- (UIView *)lt_collectionHeaderView
{
    return objc_getAssociatedObject(self, &(kCollectionHeaderViewKey));
}

#pragma mark - < 頁腳 >

/**
 設(shè)置頁腳 - set方法
 */
- (void)setLt_collectionFooterView:(UIView *)lt_collectionFooterView
{
    UIView *footerView = self.lt_collectionFooterView;
    
    if (footerView != lt_collectionFooterView) {
        // 獲取內(nèi)邊距
        UIEdgeInsets contentInset = self.contentInset;
        
        // 移除之前的
        contentInset.bottom -= footerView.height;
        [footerView removeFromSuperview];
        
        // 設(shè)置內(nèi)邊距
        contentInset.bottom += lt_collectionFooterView.height;
        self.contentInset= contentInset;
        
        // 添加頁腳
        [self addSubview:lt_collectionFooterView];
        
        objc_setAssociatedObject(self, &(kCollectionFooterViewKey), lt_collectionFooterView, OBJC_ASSOCIATION_ASSIGN);
    }
}

/**
 獲取頁腳 - get方法
 */
- (UIView *)lt_collectionFooterView
{
    return objc_getAssociatedObject(self, &(kCollectionFooterViewKey));
}

#pragma mark - < 頁眉偏移量 >

- (void)setLt_collectionHeaderViewOffset:(CGFloat)lt_collectionHeaderViewOffset
{
    objc_setAssociatedObject(self, &(kCollectionHeaderViewOffsetKey), @(lt_collectionHeaderViewOffset), OBJC_ASSOCIATION_ASSIGN);
    [self setNeedsLayout];
}

- (CGFloat)lt_collectionHeaderViewOffset
{
    NSNumber *number = objc_getAssociatedObject(self, &(kCollectionHeaderViewOffsetKey));
    return number.floatValue;
}

#pragma mark - < 頁腳偏移量 >

- (void)setLt_collectionFooterViewOffset:(CGFloat)lt_collectionFooterViewOffset
{
    objc_setAssociatedObject(self, &(kCollectionFooterViewOffsetKey), @(lt_collectionFooterViewOffset), OBJC_ASSOCIATION_ASSIGN);
    [self setNeedsLayout];
}

- (CGFloat)lt_collectionFooterViewOffset
{
    NSNumber *number = objc_getAssociatedObject(self, &(kCollectionFooterViewOffsetKey));
    return number.floatValue;
}

#pragma mark - < 交換方法 >

/**
 加載過程中, 交換方法
 */
+ (void)load
{
    Method layoutSubviews = class_getInstanceMethod(self, @selector(layoutSubviews));
    Method lt_layoutSubviews = class_getInstanceMethod(self, @selector(lt_layoutSubviews));
    method_exchangeImplementations(layoutSubviews, lt_layoutSubviews);
}

/**
 加載過程中, 交換方法
 */
- (void)lt_layoutSubviews
{
    [self lt_layoutSubviews];
    UIView *lt_collectionHeaderView = self.lt_collectionHeaderView;
    lt_collectionHeaderView.y = -lt_collectionHeaderView.height - self.lt_collectionHeaderViewOffset;
    self.lt_collectionFooterView.y = self.contentSize.height + self.lt_collectionFooterViewOffset;
}
@end

這里對于frame的設(shè)置x, y, width, height是之前已經(jīng)寫好的一個(gè)分類, 用來設(shè)置獲取view的frame, 我想這個(gè)大家都懂, 這里就直接使用了

使用方法

// 設(shè)置頁眉
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 200)];
header.backgroundColor = [UIColor purpleColor];
self.collectionView.lt_collectionHeaderView = header;
// 設(shè)置頁眉向上偏移量
self.collectionView.lt_collectionHeaderViewOffset = 0;

// 設(shè)置頁腳
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 200)];
footer.backgroundColor = [UIColor greenColor];
self.collectionView.lt_collectionFooterView = footer;
// 設(shè)置頁腳向下偏移量
self.collectionView.lt_collectionHeaderViewOffset = 0;

效果圖


最后, 如果有什么建議請?zhí)岢鰜? 我會(huì)修改

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • 解決添加到ScrollView上的UITableView控件自動(dòng)向下偏移64像素的問題 首先理解1:即使UITab...
    CoderZb閱讀 5,472評(píng)論 1 8
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,990評(píng)論 2 10
  • 著行李走出機(jī)場,夜色溫柔清冷,撲面而來,周圍一水兒的成都話,尾音甜且脆薄,飄揚(yáng)在夜色里,像是一條條輕巧游蕩的小魚兒...
    魏沈默閱讀 642評(píng)論 1 3

友情鏈接更多精彩內(nèi)容