-->

UICollectionView using sections leaves a gap betwe

2019-01-28 06:43发布

问题:

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:

But in vertical this leaves a big gap:

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

回答1:

Use like this

It will solve Gap problem

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

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 2;


回答2:

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: