4

According to many tutorials and posts like this one on Stackoverflow a UICollectionView's flow layout becomes dynamic once you set its estimatedItemSize property to a non-zero value. If this is the case the flow layout obtains each item size directly from the cells instead of asking the delegate method collectionView:layout:sizeForItemAtIndexPath:.

Unfortunately, setting the estimatedItemSize property also affects other flow layout delegate methods like collectionView:layout:insetForSectionAtIndex:. I observed that once the estimatedItemSize is set this method is simply ignored, as well as the flow layout's sectionInset property.

With this method declaration:

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

my collection view looks like this with estimatedItemSize set:

esimatedItemSize enabled

and like this without estimatedItemSize set (and returning a fixed item size CGSizeMake(300, 50) from collectionView:layout:sizeForItemAtIndexPath: instead):

esimatedItemSize disabled

Is there any reason for this kind of behavior? Why does the flow layout ignore the section insets for dynamic cell sizes?

Community
  • 1
  • 1
Mischa
  • 12,670
  • 7
  • 51
  • 94

0 Answers0