0

In the flow layout section of this UICollectionView guide in Apple documentation, under "Specifying the Space Between Items and Lines", we read:

During layout, the flow layout object adds items to the current line until there is not enough space left to fit an entire item. If the line is just big enough to fit an integral number of items with no extra space, then the space between the items would be equal to the minimum spacing. If there is extra space at the end of the line, the layout object increases the interitem spacing until the items fit evenly within the line boundaries, as shown in Figure 3-3. Increasing the spacing improves the overall look of the items and prevents large gaps at the end of each line.

(emphasis mine)

But this does not seem to be true. As of iOS 12.4 and iOS 13 beta 7, if you make a vertically scrolling collection view with small cells that don't fill the available width, the inter-item spacing does not expand from the minimum to fill the width. See this example with two sections of 5 items each, 20pt x 20pt, with minimum item spacing, minimum line spacing, and section inset all at 4 pt:

Simulator screenshot

Is the documentation outdated? Or might I be doing something weird? This is a sample app that consists of nothing but a collection view whose delegate / data source uses vanilla UICollectionViewCells that just have a background color, and implements the sizing methods mentioned above, and nothing else.

Code:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(20, 20);
}

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

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 4;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 4;
}
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • Would you please some code that you tried? – Faysal Ahmed Aug 21 '19 at 17:05
  • @FaysalAhmed posted. It's really as simple as it can be. – Tom Hamming Aug 21 '19 at 17:09
  • Based on your code the functionality is OK. Now what you want? – Faysal Ahmed Aug 21 '19 at 17:12
  • Umm... Just note from that documentation's footer `Copyright © 2014 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2014-07-15` – CenoX Aug 21 '19 at 17:12
  • @FaysalAhmed the documentation and the method naming (`minimumInteritemSpacing`) suggest that the right-most item of both rows should be 4 points in from the right side of the collection view, and the item spacing should expand evenly. – Tom Hamming Aug 21 '19 at 17:15
  • Make the number of items in the section equal to 20-25 and see if it holds true what is said in the documentation. For 5 items of size 20pt x 20pt in a section this result is correct. – Adeel Miraj Aug 21 '19 at 17:18
  • What you said in your last comment would happen if there are more items to display in the section but there’s not enough space to fix an item in the row. In your case there’s plenty of space available that’s why the interitem spacing would be the minimum inter item spacing. – Adeel Miraj Aug 21 '19 at 17:22
  • @Adeel interesting. In playing around with larger items, it does sometimes expand to the right the way I expect, but not always. The documentation seems to be unclear. – Tom Hamming Aug 21 '19 at 17:41

0 Answers0