好久没用UICollectionView,此处Mark一下。还有就是解决在window上面添加一个新的view,statusbar不显示的问题。
//
// ProductModalView.m
// guolongcustom
//
// Created by JY on 2018/1/16.
// Copyright © 2018年 jianyong.wang. All rights reserved.
//
#import "ProductModalView.h"
#import "ResourceCollectionViewCell.h"
static ProductModalView * productModal = nil;
#define COLLECTION_WIDTH (315.0 * kScreenWidth / 375 )
@interface ProductModalView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIWindow * myWindow;
@end
@implementation ProductModalView
+(ProductModalView *)shareInstance
{
static dispatch_once_t productPredicate;
dispatch_once(&productPredicate, ^{
productModal = [[self alloc]init];
});
return productModal;
}
-(instancetype)init
{
if(self = [super init]){
[self setSubView];
}
return self;
}
-(void)setSubView
{
WS(ws);
self.frame = [UIScreen mainScreen].bounds;
UIView * view = [[UIView alloc]init];
view.userInteractionEnabled = true;
view.backgroundColor = [UIColor blackColor];
view.alpha = 0.5;
[self addSubview:view];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClcik)];
[view addGestureRecognizer:tap];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(ws);
}];
//创建UICollectionViewFlowLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置滚动方向
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
//设置header大小
// layout.headerReferenceSize = CGSizeMake(COLLECTION_WIDTH, 20);
//创建CollectionView
UICollectionView * collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionview.backgroundColor = [UIColor whiteColor];
[self addSubview:collectionview];
//注册Header类
[collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
//注册自定义的CollectionViewCell
[collectionview registerNib:[UINib nibWithNibName:NSStringFromClass([ResourceCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"ResourceCollectionViewCell"];
//添加代理
collectionview.delegate = self;
collectionview.dataSource = self;
[collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.right.equalTo(ws);
make.width.mas_equalTo(COLLECTION_WIDTH);
}];
//创建window,如果用 [UIApplication sharedApplication].keyWindow; window.windowLevel = UIWindowLevelNormal;状态栏会在最上方,window.windowLevel = UIWindowLevelAlert或者statusBar;状态栏默认会隐藏,不显示;,因此此处需要自己创建window进行视图的加载
UIWindow * window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.backgroundColor = [UIColor clearColor];
window.windowLevel = UIWindowLevelAlert;
window.hidden = true;
[window addSubview:self];
self.myWindow = window;
}
#pragma mark - delegate
//collectionviewcell代理,注意与上面注册的nib文件的标识符一致
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
ResourceCollectionViewCell *cell = (ResourceCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ResourceCollectionViewCell" forIndexPath:indexPath];
return cell;
}
//每个section的上下左右边距,如果只有一个section那么久代表整个collectionview的上下左右边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 20, 0, 18);
}
//每一个cell的上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//每一个cell的左右间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//返回多少个section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 6;
}
//每个section中包含的cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return arc4random_uniform(10);
}
//每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((COLLECTION_WIDTH - 20 - 38)/3.0, 35);
}
//每个section的header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
headerView.backgroundColor =[UIColor whiteColor];
//注意此处需要移出上次添加的label
for (UIView * view in headerView.subviews) {
[view removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, COLLECTION_WIDTH-38, headerView.bounds.size.height)];
label.text = @"这是collectionView的头部";
label.font = [UIFont systemFontOfSize:15.];
[headerView addSubview:label];
return headerView;
}
return nil;
}
//每个section对应的header的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
if (section == 0) {
return CGSizeMake(COLLECTION_WIDTH, 72);
}
else {
return CGSizeMake(COLLECTION_WIDTH,64);
}
}
//显示与隐藏,注意此处是将此view添加到自己创建的window上面,因此只需要设置window的hidden属性即可
+(void)showProductModalView:(BOOL)show
{
[[ProductModalView shareInstance].myWindow setHidden:!show];
// [UIView animateWithDuration:.3 animations:^{
// UIWindow *window = [UIApplication sharedApplication].keyWindow;
// window.windowLevel = UIWindowLevelNormal;
// [window addSubview:[ProductModalView shareInstance]];
// [[ProductModalView shareInstance].myWindow setHidden:!show];
// }];
}
-(void)tapClcik
{
[[ProductModalView shareInstance].myWindow setHidden:true];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
详见代码。