UICollectionViewFlowLayoutestimatedItemSize 不能在 iOS12 上正常工作,尽管它在 iOS 11 上工作正常.* [英] UICollectionViewFlowLayout estimatedItemSize does not work properly with iOS12 though it works fine with iOS 11.*

查看:26
本文介绍了UICollectionViewFlowLayoutestimatedItemSize 不能在 iOS12 上正常工作,尽管它在 iOS 11 上工作正常.*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For UICollectionView's dynamic height cells we use,

if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}

在高度和宽度的适当限制下,它适用于 iOS 11.* 版本,但它会中断并且不会使单元格在 iOS 12.0 中动态化

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

推荐答案

在我的例子中,我通过向单元格的 contentView 显式添加以下约束来解决这个问题.

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

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

These constraints are already in place inside the xib of the cell, but somehow they aren't enough for iOS 12.

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

The other threads that suggested calling collectionView.collectionViewLayout.invalidateLayout() in various places didn't help in my situation.

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

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

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:

这篇关于UICollectionViewFlowLayoutestimatedItemSize 不能在 iOS12 上正常工作,尽管它在 iOS 11 上工作正常.*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆