集合视图单元格中的多行标签在重用后中断

Multiline label in collection view cell breaks after reuse

我有一个带有几个 UI 元素的自定义 UICollectionViewCell,在代码中使用 AutoLayout 设置进行布局。

在更大的设备(iPhone 6 及以上)上一切正常。

然而,在较小的设备上,多行 UI标签会损坏,但(似乎)只有在重复使用后才会损坏。

在初始显示中,第一个单元格如下所示:

单元格滚出屏幕并再次打开后,看起来像这样:

这些是在标签上设置的约束:

    descriptionLabel.centerXAnchor.constraint(equalTo: firstButton.centerXAnchor),
    descriptionLabel.leadingAnchor.constraint(equalTo: otherLabel.leadingAnchor),
    descriptionLabel.topAnchor.constraint(equalTo: firstButton.bottomAnchor, constant: 15),
    secondButton.topAnchor.constraint(greaterThanOrEqualTo: descriptionLabel.bottomAnchor, constant: 20),

我觉得这与 greaterThanOrEqualTo 约束有关,但如果我将其替换为普通的旧 equalTo 约束,布局就会变乱,标签会缩小到只适合一个行。

我在 UICollectionView 中遇到过类似的问题,我在 preferredMaxLayoutWidth 属性 和 widthAnchor

中找到了解决方案
productNameLabel.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.6)
productNameLabel.preferredMaxLayoutWidth = self.frame.size.width * 0.6

应该可以解决问题。