My custom collection cell contains WKWebView
. And i want to make cell height automatically to size of my WKWebView
. But all my attempts to turn on self sizing cells, failed:(
In my controller i do:
var p = self.htmlNotificationsCollectionView.collectionViewLayout as? NSCollectionViewFlowLayout
p?.estimatedItemSize = CGSize(width: self.htmlNotificationsCollectionView.frame.width, height: 120)
As documentation write then should trigger preferredLayoutAttributesFitting
method:
class HTMLNotificationCollectionViewItem: NSCollectionViewItem, WKNavigationDelegate {
override func preferredLayoutAttributesFitting(_ layoutAttributes: NSCollectionViewLayoutAttributes) -> NSCollectionViewLayoutAttributes {
print("got it")
}
}
But nothing works, method didn't call and my cells have fixed height = 120
Also i tried:
class HorizontallyFlushCollectionViewFlowLayout: NSCollectionViewFlowLayout {
override func layoutAttributesForItem(at indexPath: IndexPath) -> NSCollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? NSCollectionViewLayoutAttributes
guard let collectionView = collectionView else { return attributes }
attributes?.frame.size.width = collectionView.frame.width - sectionInset.left - sectionInset.right
return attributes
}
override func layoutAttributesForElements(in rect: CGRect) -> [NSCollectionViewLayoutAttributes] {
let allAttributes = super.layoutAttributesForElements(in: rect)
return allAttributes.flatMap { attributes in
switch attributes.representedElementCategory {
case .item: return layoutAttributesForItem(at: attributes.indexPath!)
default: return attributes
}
}
}
}
But my preferredLayoutAttributesFitting
in Item still doesn't call:
override func preferredLayoutAttributesFitting(_ layoutAttributes: NSCollectionViewLayoutAttributes) -> NSCollectionViewLayoutAttributes {
print("DSADASDADASD")
return layoutAttributes
}
Added test project: