Last active
July 12, 2019 07:46
-
-
Save giusecapo/7ac4a152488e05447e9183ffdfd9beeb to your computer and use it in GitHub Desktop.
Self-sizing UICollectionView cells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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