我在每个 UICollectionViewCell 中都有一个 WKWebView。在 web 视图完成加载其内容后,我希望我的单元格调整大小以适应内容。在此回调中:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// 1. I ask my delegate variable to notify my parent UICollectionView that this cell has finished loading its content.
// 2. collectionViewLayout gets invalidated
// 3. collectionView.layoutIfNeeded()
}
这是我的 UICollectionViewCompositionalLayout 代码:
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(1))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.edgeSpacing = .init(leading: nil, top: nil, trailing: nil, bottom: .fixed(10))
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(1))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
return section
到目前为止一切顺利,单元格正确调整大小并与 web 视图匹配。但是,当我开始滚动或点击单元格时会出现问题。这会导致一些奇怪的滚动问题,并且每个单元格的大小都不正确。我设法弄清楚这是由于上述布局代码中的“估计”属性造成的。如果我给每个单元格一个“绝对”值,就不会有任何奇怪的滚动或大小问题。
如何防止这种行为?