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) }
}