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

阿比拉姆

嗨,我是ios的初学者,在我的项目中必须UICollectionView添加添加内容UIHeader,就像我们添加UItableViewHeader一样,但是根据我的代码UIHeader没有添加,CollectionView请帮助我

我的代码:

#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
ZHZ

也许您需要返回标头的大小

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

如何在Swift中以编程方式将HeaderView从笔尖添加到UiTableView

来自分类Dev

如何在iOS中以编程方式在UITableViewCells中添加图像

来自分类Dev

如何在ListView中添加HeaderView

来自分类Dev

如何以编程方式在 UICollectionView 中为具有不同文本的不同单元格添加不同的图像?

来自分类Dev

如何在iOS中以编程方式向视频添加画外音?

来自分类Dev

如何在swift ios中以编程方式添加多个文本字段

来自分类Dev

如何以编程方式为我的UIPicker和UICollectionView优化数据输入Swift iOS

来自分类常见问题

如何在Gradle中以编程方式添加传递依赖项?

来自分类Dev

如何在Python中以编程方式添加实例?

来自分类Dev

如何在GridView中以编程方式添加更复杂的列

来自分类Dev

如何在ScrollView中以编程方式添加LinearLayout和RelativeLayout?

来自分类Dev

如何在Xcode中以编程方式添加多个UILabels?

来自分类Dev

如何在Polymer Dart中以编程方式添加扩展的LIElement?

来自分类Dev

如何在WPF中以编程方式添加或删除DisplayAttribute

来自分类Dev

如何在ScrollView中以编程方式添加LinearLayout和RelativeLayout?

来自分类Dev

如何在Android中以编程方式向画廊添加图像或以编程方式刷新画廊

来自分类Dev

如何在iOS中以编程方式创建Range Slider?

来自分类Dev

如何在iOS 7中以编程方式正确关闭UIAlertView?

来自分类Dev

如何在iOS中以编程方式滚动UICollectionViewCell?

来自分类Dev

如何在iOS中以编程方式使键盘消失

来自分类Dev

如何在iOS中以编程方式单击facebook loginView?

来自分类Dev

如何在IOS中以编程方式滚动UICollectionViewCell?

来自分类Dev

如何在iOS中以编程方式访问viber和whatsapp?

来自分类Dev

如何在UICollectionView中为Button添加addTarget?

来自分类Dev

如何使用UICollectionViewLayout以编程方式在UICollectionView上添加标头

来自分类Dev

如何快速地以编程方式在UICollectionView单元上添加UIView

来自分类Dev

如何以编程方式将UICollectionView添加到容器?

来自分类Dev

以编程方式在iOS中添加导航栏

Related 相关文章

  1. 1

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

  2. 2

    如何在Swift中以编程方式将HeaderView从笔尖添加到UiTableView

  3. 3

    如何在iOS中以编程方式在UITableViewCells中添加图像

  4. 4

    如何在ListView中添加HeaderView

  5. 5

    如何以编程方式在 UICollectionView 中为具有不同文本的不同单元格添加不同的图像?

  6. 6

    如何在iOS中以编程方式向视频添加画外音?

  7. 7

    如何在swift ios中以编程方式添加多个文本字段

  8. 8

    如何以编程方式为我的UIPicker和UICollectionView优化数据输入Swift iOS

  9. 9

    如何在Gradle中以编程方式添加传递依赖项?

  10. 10

    如何在Python中以编程方式添加实例?

  11. 11

    如何在GridView中以编程方式添加更复杂的列

  12. 12

    如何在ScrollView中以编程方式添加LinearLayout和RelativeLayout?

  13. 13

    如何在Xcode中以编程方式添加多个UILabels?

  14. 14

    如何在Polymer Dart中以编程方式添加扩展的LIElement?

  15. 15

    如何在WPF中以编程方式添加或删除DisplayAttribute

  16. 16

    如何在ScrollView中以编程方式添加LinearLayout和RelativeLayout?

  17. 17

    如何在Android中以编程方式向画廊添加图像或以编程方式刷新画廊

  18. 18

    如何在iOS中以编程方式创建Range Slider?

  19. 19

    如何在iOS 7中以编程方式正确关闭UIAlertView?

  20. 20

    如何在iOS中以编程方式滚动UICollectionViewCell?

  21. 21

    如何在iOS中以编程方式使键盘消失

  22. 22

    如何在iOS中以编程方式单击facebook loginView?

  23. 23

    如何在IOS中以编程方式滚动UICollectionViewCell?

  24. 24

    如何在iOS中以编程方式访问viber和whatsapp?

  25. 25

    如何在UICollectionView中为Button添加addTarget?

  26. 26

    如何使用UICollectionViewLayout以编程方式在UICollectionView上添加标头

  27. 27

    如何快速地以编程方式在UICollectionView单元上添加UIView

  28. 28

    如何以编程方式将UICollectionView添加到容器?

  29. 29

    以编程方式在iOS中添加导航栏

热门标签

归档