0

I tried following this solution (How to determine height of UICollectionView with FlowLayout) to determine the size of my UICollectionViewLayout in order to size cells accordingly. My code below calls the collectionViewContentSize, which throws an error upon being called:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {


    let frameWidth  = collectionViewLayout.collectionViewContentSize.width
    let maxCellWidth = (frameWidth) / (7.0/6.0 + 6.0)
    let frameHeight  = collectionViewLayout.collectionViewContentSize.height
    let maxCellHeight = frameHeight

    let cellEdge = maxCellWidth < maxCellHeight ? maxCellWidth : maxCellHeight

    return CGSize(width: cellEdge, height: cellEdge)
}

The error is the Thread 1 SIGABRT.

Any idea why this does not work?

Alex Kornhauser
  • 444
  • 4
  • 13
  • 34

1 Answers1

1

You are probably getting infinite recursion because in order to know the content size of the collection view, it needs to know the size of each item. The whole reason the collection view is calling sizeForItemAt is to determine the content size of the collection view.

The good news is that there is no reason for you to ask for the collection view's content size. Instead, base your item's size on the size of the collection view itself, not its content size.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let frameWidth  = collectionView.frame.width
    let maxCellWidth = (frameWidth) / (7.0/6.0 + 6.0)
    let frameHeight  = collectionView.frame.height
    let maxCellHeight = frameHeight

    let cellEdge = maxCellWidth < maxCellHeight ? maxCellWidth : maxCellHeight

    return CGSize(width: cellEdge, height: cellEdge)
}
rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • Your answer works but then it leads to another problem. I call flowLayout.itemSize.width in collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) and it now returns the wrong size.. ? – Alex Kornhauser Jun 22 '18 at 03:30
  • You really should avoid basing layout sizes on other layout sizes. – rmaddy Jun 22 '18 at 03:34
  • Once I set the size, what should I base the size and spacing on in another callback? – Alex Kornhauser Jun 22 '18 at 03:36
  • It depends on your needs. My answer assumes you want the cell size based on the visible size of the collection view itself. – rmaddy Jun 22 '18 at 03:37
  • i want to base the cell size on the size of the collectioview on the screen. I would like to base the spacing of the cells on the size of the cells, so I guess I'm not sure what flowLayout.itemSize and flowLayout.minimumInteritemSpacing correspond to – Alex Kornhauser Jun 22 '18 at 03:42
  • OK, my answer gives you your desired item size (at least it's based on the collection view's size. I no idea if your width calculation is what you really want or not, that's up to you). Base the spacing on the same basic calculation - use the collection view's frame. – rmaddy Jun 22 '18 at 03:47