58

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!

etolstoy
  • 1,798
  • 21
  • 33
Dario
  • 1,315
  • 1
  • 11
  • 24

4 Answers4

191

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

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • 1
    I have added and the content looks scrollable, but only bounces with the visible content. The content off the edge will not scroll into view. Any more missing settings? – mobibob Sep 16 '15 at 21:17
12

With storyboards in attributes inspector for collection view "Bounces" and "Bounces Vertically" should be checked.

Vladimir Shutyuk
  • 2,956
  • 1
  • 24
  • 26
6

Setting the height of the UICollectionView to size of UIView will make your scrolling problem disabled. If the UICollectionView is 568 pixels tall then it will only ever need to scroll if it has more than 568 pixels worth of content in it. You should set it to the height of the view it is contained in (same as the width).

Hope it helps you.

etolstoy
  • 1,798
  • 21
  • 33
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • adding constraint in IB to keep UICollectionView edges same as its superview's edges solved my issue – Colin Jul 09 '14 at 14:05
1

You can use these codes for Swift 5.

    collectionView.isScrollEnabled = true
    collectionView.isUserInteractionEnabled = true
    collectionView.alwaysBounceVertical = true
Celil Bozkurt
  • 1,693
  • 16
  • 18