For UICollectionView's dynamic height cells we use,
if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout { layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize }
with the proper constraint of height and width, it works fine with iOS 11.* versions but it breaks and does not make the cells dynamic for iOS 12.0
A layout object that organizes items into a grid with optional header and footer views for each section.
Make-CollectionView-Height-Dynamic-According-to-ContentAdd a height constraint to your collection view. Set its priority to 999. Set its constant to any value that only makes it reasonably visible on the storyboard. Change the bottom equal constraint of the collection view to greater or equal.
An object that manages an ordered collection of data items and presents them using customizable layouts.
In my case, I solved this by explicitly adding the following constraints to the cell's contentView.
class Cell: UICollectionViewCell { // ... override func awakeFromNib() { super.awakeFromNib() // Addresses a separate issue and prevent auto layout warnings due to the temporary width constraint in the xib. contentView.translatesAutoresizingMaskIntoConstraints = false // Code below is needed to make the self-sizing cell work when building for iOS 12 from Xcode 10.0: let leftConstraint = contentView.leftAnchor.constraint(equalTo: leftAnchor) let rightConstraint = contentView.rightAnchor.constraint(equalTo: rightAnchor) let topConstraint = contentView.topAnchor.constraint(equalTo: topAnchor) let bottomConstraint = contentView.bottomAnchor.constraint(equalTo: bottomAnchor) NSLayoutConstraint.activate([leftConstraint, rightConstraint, topConstraint, bottomConstraint]) } }
These constraints are already in place inside the xib of the cell, but somehow they aren't enough for iOS 12.
The other threads that suggested calling collectionView.collectionViewLayout.invalidateLayout()
in various places didn't help in my situation.
Sample code here: https://github.com/larrylegend/CollectionViewAutoSizingTest
This applies the workaround to code from a tutorial by https://medium.com/@wasinwiwongsak/uicollectionview-with-autosizing-cell-using-autolayout-in-ios-9-10-84ab5cdf35a2:
Based on ale84
's answer and because of the fact I needed that iOS 12 fix in multiple places I created a UICollectionViewCell extension which I named UICollectionViewCell+iOS12:
extension UICollectionViewCell { /// This is a workaround method for self sizing collection view cells which stopped working for iOS 12 func setupSelfSizingForiOS12(contentView: UIView) { contentView.translatesAutoresizingMaskIntoConstraints = false let leftConstraint = contentView.leftAnchor.constraint(equalTo: leftAnchor) let rightConstraint = contentView.rightAnchor.constraint(equalTo: rightAnchor) let topConstraint = contentView.topAnchor.constraint(equalTo: topAnchor) let bottomConstraint = contentView.bottomAnchor.constraint(equalTo: bottomAnchor) NSLayoutConstraint.activate([leftConstraint, rightConstraint, topConstraint, bottomConstraint]) } }
And then in your collection view cells we do something like this (if your cell is created in IB):
override func awakeFromNib() { super.awakeFromNib() if #available(iOS 12, *) { setupSelfSizingForiOS12(contentView: contentView) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With