Enable bounce / scroll for UICollectionView while no cells

Due to some goals (e.g. pull to refresh) I need the UICollectionView to bounce or scroll when there are no cells - means numberOfItemsInSection: return 0

I have my code:

 ... 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 and scroll. I suspect this is due to an empty cell, but I need to enable bounce or scroll when there is no cell. This seems like another problem: SVPullToRefresh cannot pull an empty UICollectionView

+6
source share
3 answers
  - (void)_edgeInsetsToFit { UIEdgeInsets edgeInsets = self.collectionView.contentInset; CGSize contentSize = self.collectionView.contentSize; CGSize size = self.collectionView.bounds.size; CGFloat heightOffset = (contentSize.height + edgeInsets.top) - size.height; if (heightOffset < 0) { edgeInsets.bottom = size.height - (contentSize.height + edgeInsets.top) + 1; self.collectionView.contentInset = edgeInsets; } else { edgeInsets.bottom = 0; self.collectionView.contentInset = edgeInsets; } } 

I called this method after layoutSubviews

 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; _STAssetViewController *weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf _edgeInsetsToFit]; }); } 

You can try this way

-3
source

Do it like this:

 self.collectionView.alwaysBounceVertical = YES; 

answer to @VNJ on here

+39
source

Change this parameter in the storyboard:

enter image description here

+13
source

Source: https://habr.com/ru/post/983562/


All Articles