UICollectionView标头视图以编程方式创建并添加? [英] UICollectionView header view create programmatically and add?

查看:136
本文介绍了UICollectionView标头视图以编程方式创建并添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在情节提要中创建了UICollectionView并添加了页眉页脚视图,但我的问题是如何以编程方式创建UICollectionViewReusable视图以将其添加为SupplementaryView.我尝试了但未调用委托.请注意,我还设置了委托.我尝试过的代码

I have created UICollectionView in storyboard and added header footer view its working fine.But My question is how to create UICollectionViewReusable view to add as SupplementaryView programatically.I tried but delegates not called.Please note that i have set delegate also.below code i have tried

- (void)setUpCustomCollectionView
{

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, 320, 500) collectionViewLayout:layout];

    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView"];

    self.collectionView.bounces = NO;
    self.collectionView.tag = 10;
    self.collectionView.backgroundColor = [UIColor darkGrayColor];
    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;

    [self.baseScrollView addSubview:self.collectionView];
}

在代理人

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath
{
 if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView" forIndexPath:indexPath];

            UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 80)];
            view.backgroundColor = [UIColor redColor];

                 [headerView addSubview:view];

            return headerView;
        }
}

引导我.

推荐答案

我刚刚遇到了一个类似的问题,即未调用以下代表...

I have just been having a similar issue where the following delegate wasn't being called...

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

然后,我记得当我定义UICollectionViewFlowLayout的实例时,我已经按照以下代码分配了itemSize值...

Then I remembered that when I was defining an instance of the UICollectionViewFlowLayout, I had assigned the itemSize value as per following code...

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(106.f, 106.f);

还尝试在标题中添加以下行...

Try also adding the following line to it as well for the header...

layout.headerReferenceSize = CGSizeMake(320.f, 30.f);

这篇关于UICollectionView标头视图以编程方式创建并添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆