Skip to content

Instantly share code, notes, and snippets.

@giusecapo
Last active July 12, 2019 07:46
Show Gist options
  • Save giusecapo/7ac4a152488e05447e9183ffdfd9beeb to your computer and use it in GitHub Desktop.
Save giusecapo/7ac4a152488e05447e9183ffdfd9beeb to your computer and use it in GitHub Desktop.
Self-sizing UICollectionView cells
import UIKit
class ViewController: UIViewController {
// CollectionView from Storyboard
@IBOutlet weak var myCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
myCollectionView.delegate = self
myCollectionView.dataSource = self
// Register the Xib for cell
let nib = UINib(nibName: "myCustomCell", bundle: nil)
myCollectionView.register(nib, forCellWithReuseIdentifier: "reuseId")
// IMPORTANT: this is the key to make your cells auto-sizing
if let collectionViewLayout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
collectionViewLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment