gpt4 book ai didi

swift - UICollectionViewCell 不强制单元格大小

转载 作者:行者123 更新时间:2023-11-28 13:25:24 25 4
gpt4 key购买 nike

我正在使用

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 400, height: 600)
}

指定我的 CollectionView 的单元格大小。它工作正常,但只要我将宽度约束 (UILabel.width == ContentView.width) 添加到 UILabelContentView单元格,单元格的宽度缩小到 UILabel 的固有宽度。

我正在使用 UICollectionViewDelegateFlowLayout 和水平滚动。

如何强制 ContentView 保持我指定的单元格大小并让 subview 遵循自动布局约束?

最佳答案

问题是在 Storyboard 中,您的 Collection View 的估计大小配置为自动:

enter image description here

如果您希望遵守您的sizeForItemAt 实现而不是内部约束,则需要将其设置为 None。

关于swift - UICollectionViewCell 不强制单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com