Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable bounce/scroll for UICollectionView while no cells

Tags:

ios

Due to some purposes(like pull to refresh), I need a UICollectionView can bounce or scroll when there is no cells -- means numberOfItemsInSection: return 0

I have my code like this:

...
        UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = SPLIT_SPACE;
        flowLayout.minimumLineSpacing = SPLIT_SPACE;
        targetCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - TABBAR_HEIGHT)
                                                    collectionViewLayout:flowLayout];
        [targetCollection registerNib:[UINib nibWithNibName:@"DashboardCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DashboardCell"];

        targetCollection.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        targetCollection.backgroundColor = [UIColor colorWithHex:@"#EAEAEA"];
        targetCollection.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
        targetCollection.alwaysBounceVertical = YES; 
...
    #pragma mark - UICollectionViewDataSource
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
    {
        return 0;
    }

However, when testing, this empty UICollectionView cannot bounce nor scroll. I suspect it is related to empty cell, but I do need to enable the bounce or scroll when no cell. This is similar to another of my problem:SVPullToRefresh cannot pull on an empty UICollectionView

like image 249
Wingzero Avatar asked Mar 10 '15 03:03

Wingzero


3 Answers

Do it like this:

self.collectionView.alwaysBounceVertical = YES;

answer by @VNJ at here

like image 196
Wellbin Huang Avatar answered Nov 15 '22 20:11

Wellbin Huang


Change this setting in your storyboard:

enter image description here

like image 41
Yuchen Avatar answered Nov 15 '22 22:11

Yuchen


Swift Solution

collectionView.alwaysBounceVertical = true
like image 1
Kirill Kudaev Avatar answered Nov 15 '22 21:11

Kirill Kudaev