简体   繁体   English

如何在iOS中以编程方式为UICollectionView添加HeaderView

[英]How to add HeaderView programmatically for UICollectionView in ios

Hi i am beginner in ios and in my project have to create UICollectionView with adding UIHeader as like how we are adding UItableView Header but according to my code UIHeader is not adding for CollectionView please help me some one 嗨,我是ios的初学者,在我的项目中必须添加UIHeader来创建UICollectionView ,就像我们添加UItableView Header一样,但是根据我的代码, UIHeader没有为CollectionView添加,请帮助我

my code:- 我的代码:

#import "ViewController.h"

@interface ViewController ()
{
     UICollectionView *_collectionView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, 320, 480) collectionViewLayout:layout];

    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

    [_collectionView setBackgroundColor:[UIColor clearColor]];
    self.view.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:_collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 15;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    cell.backgroundColor=[UIColor greenColor];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(50, 50);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    UIEdgeInsets insets=UIEdgeInsetsMake(10, 10, 10, 10);
    return insets;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (kind == UICollectionElementKindSectionHeader) {

        UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        reusableview.backgroundColor = [UIColor redColor];

        if (reusableview==nil) {

            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        }

        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 10)];
        label.text= @"Hellow orld";
        [reusableview addSubview:label];
        return reusableview;
    }
    return nil;
}

@end

it may help you 它可以帮助你

self.collectionView  =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) collectionViewLayout:flowlayout];
self.collectionView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
UIView *headerView = [[UIView alloc]init];
headerView.frame = CGRectMake(0, -50, 320, 50);
[self.collectionView addSubview: headerView];
[self.view addSubview: _collectionView];

Maybe you need return the size of the header in 也许您需要返回标头的大小

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: MyFlowLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: xx, height: xx)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UICollectionView中添加UICollectionView(以编程方式) - How to add UICollectionView in UICollectionView (programmatically) 如何像 UITableView 的 tableHeaderView 一样在 UICollectionView 中添加 HeaderView - How to add HeaderView in UICollectionView like UITableView's tableHeaderView 如何在 Swift 中以编程方式将 HeaderView 从 nib 添加到 UiTableView - How to Add HeaderView from nib to UiTableView programmatically in Swift 如何以编程方式将UICollectionView添加到容器? - How to add a UICollectionView to a container programmatically? 如何使用UICollectionViewLayout以编程方式在UICollectionView上添加标头 - How to programmatically add header on UICollectionView with UICollectionViewLayout 如何以编程方式在UICollectionView上添加自动布局? - How to add auto layout on UICollectionView programmatically? ios UICollectionView以编程方式删除标头 - ios UICollectionView remove header programmatically 如何在不使用情节提要的情况下在iOS Swift 3中以编程方式创建UICollectionView类? - How to make UICollectionView class programmatically in iOS Swift 3 without using storyboard? 如何以编程方式为我的 UIPicker 和 UICollectionView 优化数据输入 Swift iOS - How to optimise data inputs for my UIPicker and UICollectionView programmatically Swift iOS 具有自定义布局的UICollectionView - 以编程方式添加补充视图的方式/位置? - UICollectionView with custom layout - how/where to add a supplementary view programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM