3

So I'm trying to figure out how to self size collection view cells. So I have a ViewController and I dragged a collectionView inside the ViewController. I am not using a xib file, I am using a prototype cell. Now I can't seem to figure out how to self size the cell. It's not as easy as the table view. I've tried the following that I found.

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {

        layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100)
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    }

Then I found another one that creates a dummyCell, that looks something like this. This crashes my app though.

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

    let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
    let dummyCell = CommentCell(frame: frame)
    let comment = comments[indexPath.item]
    print(comment.text)

    dummyCell.comment = comment
    dummyCell.layoutIfNeeded()

    let targetSize = CGSize(width: view.frame.width, height: 1000)
    let estimateSize = dummyCell.systemLayoutSizeFitting(targetSize)
    let height = max(45 + 8 + 8, estimateSize.height)
    return CGSize(width: view.frame.width, height: height)

}

So would I need a collection view controller instead of a view controller. How would I be able to self size cells without using a collectionViewController or a nib file? Thanks

qtmfld
  • 2,606
  • 2
  • 16
  • 33
Luis F Ramirez
  • 158
  • 1
  • 10
  • it may help, Please check this link, https://stackoverflow.com/questions/25895311/uicollectionview-self-sizing-cells-with-auto-layout – Caleb Jan 08 '19 at 08:13
  • "This crashes my app though” ... If you want to use that latter technique and if it is crashing, you’ll have to tell us on which line it crashed, what showed up on the console, etc. We can’t easily diagnose without that sort of information... – Rob Jan 08 '19 at 21:22
  • Take a look at this post https://medium.com/dev-genius/take-your-ios-architecture-to-the-next-level-with-cellconfigurator-7a645940ff68 – Feridun Erbaş Sep 27 '20 at 16:56

1 Answers1

1

Assuming you have constraints in the cell NIB/prototype, you want to set itemSize to .automaticSize:

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100)
    layout.itemSize = UICollectionViewFlowLayout.automaticSize
}

You haven’t shared where you are setting this, but if done in viewDidLoad, the view.frame.width might not be what you think it is. You will want to double check that...

Rob
  • 371,891
  • 67
  • 713
  • 902