UICollectionView 不滚动 [英] UICollectionView does not scroll

查看:31
本文介绍了UICollectionView 不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView 设置了一个 UICollectionViewDataSource 目前提供六个项目.这些比填满屏幕所需的要少.问题是我的收藏视图只有在有足够的项目填满屏幕时才会滚动(用 10、20 进行测试).当显示较少的项目时,它甚至不会做我想要的弹跳动画,它只是固定的.

- (void)viewWillAppear:(BOOL)animated{[超级viewWillAppear:动画];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionViewData) name:UIDocumentStateChangedNotification object:nil];UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];flowLayout.itemSize = CGSizeMake(160, 100);flowLayout.minimumInteritemSpacing = 0;flowLayout.minimumLineSpacing = 0;self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];[self.collectionView registerClass:[UICollectionViewCell 类] forCellWithReuseIdentifier:@"Cell"];self.collectionView.delegate = self;self.collectionView.dataSource = self;self.collectionView.bounces = YES;[self.view addSubview:self.collectionView];}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {返回 [self.collectionViewData 计数];}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];费用 *expense = [self.collectionViewData objectAtIndex:indexPath.row];UILabel *label = [[UILabel alloc]initWithFrame:cell.bounds];标签.文本=费用.值;label.backgroundColor = [UIColor clearColor];label.font = [UIFont fontWithName:@"Miso-Bold" size:30];label.textAlignment = NSTextAlignmentCenter;[单元格添加子视图:标签];cell.backgroundColor = [UIColor colorWithRed:1 - (indexPath.row/30.0f) green:0 blue:1 alpha:1];返回单元格;}

感谢您的帮助!

解决方案

bounces,尽管它的名字,不是正确的属性设置.您还需要设置 alwaysBounceVertical 和/或 alwaysBounceHorizo​​ntal.来自文档:

<块引用>

如果此属性设置为 YES 并且bounces 为YES,则允许垂直拖动即使内容小于滚动视图的边界.默认值为 NO.

<小时>

注意 IB 中令人困惑的名称 .. https://stackoverflow.com/a/18391029/294884p>

I have a UICollectionView set up with a UICollectionViewDataSource that currently provides six items. These are fewer than needed to fill the screen. The problem is that my collection view only scrolls when there are enough items to fill the screen (tested with 10, 20). When displaying fewer items it wont even do this bounce animation I am trying to get, it's just fixed.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionViewData) name:UIDocumentStateChangedNotification object:nil];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(160, 100);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;

    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = YES;
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.collectionViewData count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    Expense *expense = [self.collectionViewData objectAtIndex:indexPath.row];

    UILabel *label = [[UILabel alloc]initWithFrame:cell.bounds];
    label.text = expense.value;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"Miso-Bold" size:30];
    label.textAlignment = NSTextAlignmentCenter;
    [cell addSubview:label];

    cell.backgroundColor = [UIColor colorWithRed:1 - (indexPath.row / 30.0f) green:0 blue:1 alpha:1];

    return cell;
}

Thanks for your help!

解决方案

bounces, despite it's name, isn't the right property to set. You also need to set alwaysBounceVertical and / or alwaysBounceHorizontal. From the documentation:

If this property is set to YES and bounces is YES, vertical dragging is allowed even if the content is smaller than the bounds of the scroll view. The default value is NO.


Note the confusing name in IB .. https://stackoverflow.com/a/18391029/294884

这篇关于UICollectionView 不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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