UICollectionView不会滚动

Dar*_*rio 53 iphone ios uicollectionview

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

jrt*_*ton 172

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

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


请注意IB中令人困惑的名称.. /sf/answers/1287372061/


Vla*_*yuk 10

使用属性检查器中的故事板进行集合视图"Bounces"和"Bounces Vertically"应该被检查.


Nis*_*agi 5

将高度设置UICollectionView为大小UIView将使您的滚动问题被禁用。如果UICollectionView568 像素高,那么它只需要在其中包含超过 568 像素的内容时才需要滚动。您应该将其设置为包含它的视图的高度(与宽度相同)。

希望对你有帮助。