具有动态图像尺寸的 UICollectionViewCell - 当您滚动时,当单元格被重新使用时,图像将失去其初始约束附加的 TestApp

UICollectionViewCell with a dynamic image dimensions - As u scroll, the image loses its initial constraints when the cell is re-used TestApp attached

我需要一些帮助。

我有一个用于入职场景的 UICollectionViewCell,每个单元格包含一张动态设置的不同尺寸的图像。

我正在使用类似于 SnapKit 的 WWLayout 来处理约束。

问题是当我轻扫并重新使用单元格时,约束中断并且图像失去其初始尺寸。

我附上了一个TestApp。

https://www.dropbox.com/s/tx141lpr0uzlhbx/Test%20App.Image.Resize.zip?dl=0

谢谢。

图像未正确缩放。使用此代码更新您的代码以防止图像拉伸。

private lazy var imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    return imageView
}()

这个变量在OnBoardingItemView.swift,第35行

OnBoardingItemView.swift 函数 layoutUI() 中,就在布局 imageView 之前,尝试添加此行:

imageView.removeConstraints(imageView.constraints)