UICollectionView- How to remove the spacing above the first cell?

UICollectionView- How to remove the spacing above the first cell?

I have top insert = 0 and rows = 10

enter image description here

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { return CGSizeMake(collectionView.width, 44); } return CGSizeMake(300, 150); } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 10, 10, 10); } 

This seems to be due to setting the first cell to a different size

if I remove the check for row == 0 in sizeForItemAtIndexPath and return it for all cells, it is at the same level as the top one.

Any ideas?

+4
source share
5 answers

The solution is to simply write inside viewDidLoad

 self.automaticallyAdjustsScrollViewInsets = NO; 
+10
source

I did it as follows (tested on iOS7, 55 - magic value):

 #pragma mark - UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { //-55 is a tweak value to remove top spacing return CGSizeMake(1, -55); } 

Alternative way to use your code:

 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(-55, 10, 10, 10); } 
+8
source

If you want to use Interface Builder, you can clear the "Configure Scroll Inserts" checkbox in the view controller attribute inspector:

Uncheck here.

Or use the answer "Amit" to do this programmatically.

+7
source

I do not think this is your top insert. Modify your question with some piece of code that you applied.

If it was a collectionView insert, then it should be removed by passing 0 as the top insert.

 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 10, 10, 10); } 

And the next layout delegate used for line spacing.

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 30; } 
+1
source

Using srtuct and spring, set the autoresist mask of the collection like this, and also make sure the y frame is set to 0

enter image description here

0
source

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


All Articles