UICollectionView不会滚动 [英] UICollectionView does not scroll

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

问题描述

我有一个 UICollectionView 设置了 UICollectionViewDataSource ,目前提供六个项目。
这些比填充屏幕所需要的少。问题是我的集合视图只在有足够的项目填充屏幕时滚动(用10,20测试)。
当显示较少的项目时,它甚至不会做我想要的弹跳动画,它只是修复。

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;
}

感谢您的帮助!

推荐答案

跳出,尽管它的名称,但不是正确的属性。您还需要设置 alwaysBounceVertical 和/或 alwaysBounceHorizo​​ntal 。从文档中:

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


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






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

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

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