2013-04-17 68 views
45

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

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

感谢您的帮助!

回答

151

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

如果这个属性设置为YES和反弹是YES,垂直允许拖动即使含量大于滚动视图的范围较小。默认值是NO。


注意,在IB混乱的名字.. https://stackoverflow.com/a/18391029/294884

+0

这是正确的答案 - OP应该接受这个答案。 –

+0

伟大的技巧感谢 – Fattie

+0

谢谢,它像一个魅力:) – evya

4

UICollectionView的高度设置为UIView的大小会使您的滚动问题被禁用。如果UICollectionView的像素高度为568像素,则只有在其内容超过568像素的情况下才需要滚动。您应该将其设置为其所在视图的高度(与宽度相同)。

希望它可以帮助你。

+0

在IB中添加约束以保持UICollectionView边缘与其超级视图的边缘相同解决了我的问题 – Colin

9

随着属性检查器集合视图“退回”和“退回垂直”故事板应该进行检查。