iOS 设置UICollectionView的sectionHeader


#import "ZYStudyLikeCollectionView.h"
#import "LKStudyLikeCollectionViewCell.h"
#import "ZYStudyCollectionReusableView.h"

@interface ZYStudyLikeCollectionView()

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation ZYStudyLikeCollectionView


- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2, (SCREEN_WIDTH/2-10)*9/16+70);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    //第一种方式   设置collectionView的sectionheader尺寸
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 44);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LKStudyLikeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LKStudyLikeCollectionViewCell"];
    [self.collectionView registerClass:[ZYStudyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYStudyCollectionReusableView"];
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LKStudyLikeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LKStudyLikeCollectionViewCell" forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}


//第二种方式   设置collectionView的sectionheader尺寸
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(SCREEN_WIDTH, 44);
//}


//sectionheader
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    ZYStudyCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYStudyCollectionReusableView" forIndexPath:indexPath];
    
    return headView;
}


sectionHeader


#import 

NS_ASSUME_NONNULL_BEGIN

@interface ZYStudyCollectionReusableView : UICollectionReusableView

@end

#import "ZYStudyCollectionReusableView.h"

@interface ZYStudyCollectionReusableView()

@property (strong, nonatomic) UILabel *titleLab;

@end

@implementation ZYStudyCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        
        if (!self.titleLab) {
            self.titleLab = ({
                UILabel *titleLab = [[UILabel alloc] init];
                titleLab.textColor = [UIColor colorWithHexString:@"0x363636"];
                titleLab.font = [UIFont systemFontOfSize:17];
                titleLab.text = @"猜你喜欢";
                [self addSubview:titleLab];
                [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self).offset(5);
                    make.centerY.equalTo(self);
                    
                }];
                titleLab;
            });

        }
    }
    return self;
}

@end

你可能感兴趣的:(iOS 设置UICollectionView的sectionHeader)