Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView using sections leaves a gap between cells

I'm using a UICollectionView to show several sections of data. These sections have a fixed number of items. I want all the items to show in a continuous grid.

Right now I accomplish this in horizontal orientation: enter image description here

But in vertical this leaves a big gap: enter image description here

I want to solve this gap between sections because it's ugly and it doesn't belong there. I'd be happy to use a custom FlowLayout, but I can't find a tutorial that points me in the right direction (I've found several, but none of them really touch this problem specifically.)

Can anybody help me solve this problem, or at least point me in the right direction?

P.S: I've implemented sections because I'm loading the data on the fly. Using 1 section isn't an option for me at this moment.

UPDATE On request I'm adding the values used for my current FlowLayout. I'm using the standard Horizontal Flow Layout on a fullscreen (minus UINavigationBar) UICollectionView with 21 items per section.

  • Scroll direction: Horizontal
  • Cell size: 248, 196
  • Header / footer size: none
  • Min spacing for cells: 10
  • Min spacing for lines: 10
  • Section Insets: 20, 20, 10, 10
like image 206
Jake Avatar asked Jan 14 '23 05:01

Jake


2 Answers

Use like this

It will solve Gap problem

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 2;
like image 144
Nagendra Tripathi Avatar answered Feb 04 '23 10:02

Nagendra Tripathi


To remove those gaps you need to create custom layout which will act as layout for your collection view. This class will child class for UICollectionViewFlowLayout.

Then you can override below two methods and can create your own custom layout as you want.

  - (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
  - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path

UICollectionViewLayoutAttributes is class which will deal with cell position, frame, Zindex etc

You can also use below properties.

 collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
 collectionView:layout:minimumLineSpacingForSectionAtIndex:
like image 24
Swapnil Avatar answered Feb 04 '23 12:02

Swapnil