UICollectionViewFlowLayout estimatedItemSize与iOS12无法正常工作,但它适用于iOS 11*

Ama*_*pta 30 uicollectionview uicollectionviewlayout swift uicollectionviewflowlayout ios12

对于我们使用的UICollectionView动态高度单元,

if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
Run Code Online (Sandbox Code Playgroud)

对于高度和宽度的适当约束,它适用于iOS 11.*版本,但它破坏并且不会使iOS 12.0的单元格动态

ale*_*e84 65

在我的例子中,我通过明确地将以下约束添加到单元格的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])
    }
}
Run Code Online (Sandbox Code Playgroud)

这些约束已经存在于单元格的xib中,但不知何故,它们对iOS 12来说还不够.

建议collectionView.collectionViewLayout.invalidateLayout()在各个地方打电话的其他线程对我的情况没有帮助.

示例代码:https://github.com/larrylegend/CollectionViewAutoSizingTest

这将解决方法应用于https://medium.com/@wasinwiwongsak/uicollectionview-with-autosizing-cell-using-autolayout-in-ios-9-10-84ab5cdf35a2教程中的代码:

  • 你是怎么想出来的?我不认为Apple在任何地方都提到了这个巨大的突破性变化 令人遗憾的是,他们让我们用这样的样板垃圾污染我们的应用程序代码. (7认同)
  • 谢谢,有同样的问题,这对我现在有用.而且我只为iOS12调用此代码:if #available(iOS 12.0,*){} (2认同)
  • @RajanMaharjan嗨,从您的示例来看,我认为您应该将布局的“ itemSize”设置为“ UICollectionViewFlowLayout.automaticSize”,并将“ estimatedItemSize”设置为估计值。 (2认同)

Vas*_*rov 7

基于ale84答案,因为我需要在多个地方修复iOS 12,我创建了一个名为UICollectionViewCell + iOS12的UICollectionViewCell扩展:

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])
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的集合视图单元格中,我们执行类似这样的操作(如果您的单元格是在IB中创建的):

override func awakeFromNib() {
    super.awakeFromNib()
    if #available(iOS 12, *) { setupSelfSizingForiOS12(contentView: contentView) }
}
Run Code Online (Sandbox Code Playgroud)


CDM*_*CDM -1

我也有同样的问题。使用 UILabels 来调整集合视图的大小。如果我collectionView.collectionViewLayout.invalidateLayout()在重新加载数据之前运行,它可以很好地调整标签的大小。

还是不太对劲。我很难弄清楚,因为我是在模拟器与设备上运行它。