Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView- How to remove spacing above first cell?

UICollectionView- How to remove spacing above first cell?

I have the top inset = 0, and lines = 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);
}

Seems it is something to do with setting the first cell to a different size

if i remove the check for row ==0 in sizeForItemAtIndexPath and return same for all cells it is flush with the top.

Any ideas?

like image 427
tiltem Avatar asked Nov 27 '22 17:11

tiltem


2 Answers

Solution is just to write inside viewDidLoad

self.automaticallyAdjustsScrollViewInsets = NO;
like image 78
Amit Saxena Avatar answered Dec 05 '22 15:12

Amit Saxena


I did it following way (tested on iOS7, 55 is a 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 using your code:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(-55, 10, 10, 10);
}
like image 20
SoftDesigner Avatar answered Dec 05 '22 13:12

SoftDesigner