CollectionViewCell 的自动布局:Trailing/Leading 约束更改单元格大小
Auto-Layout for CollectionViewCell: Trailing/Leading constraints change cell size
我只是不熟悉 Xcode 中的自动布局。当我为 Header Stack View、Bottom Stack View 或它们之间的图像设置尾部和前导约束时,我的单元格的大小 (height/width) 会发生变化。为什么会发生,我该如何避免?我认为我构建布局的类型有些不对。我非常感谢任何帮助。
我用这段代码设置单元格大小:
extension MainVC: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = view.frame.size.height
let width = view.frame.size.width
return CGSize(width: width * 0.42, height: height * 0.3)
}
}
我的单元格布局:
这是当我对底部堆栈视图使用约束 时应用程序的外观。
对于底部堆栈视图(正确的单元格大小),没有约束。
单元格现在可以 self-size 使用自动布局并忽略 return 在 collectionView(_:layout:sizeForItemAt:)
中的内容。如果要使用 collectionView(_:layout:sizeForItemAt:)
,请在故事板中将 Estimate size
更改为 None
。
我只是不熟悉 Xcode 中的自动布局。当我为 Header Stack View、Bottom Stack View 或它们之间的图像设置尾部和前导约束时,我的单元格的大小 (height/width) 会发生变化。为什么会发生,我该如何避免?我认为我构建布局的类型有些不对。我非常感谢任何帮助。
我用这段代码设置单元格大小:
extension MainVC: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = view.frame.size.height
let width = view.frame.size.width
return CGSize(width: width * 0.42, height: height * 0.3)
}
}
我的单元格布局:
这是当我对底部堆栈视图使用约束 时应用程序的外观。
对于底部堆栈视图(正确的单元格大小),没有约束。
单元格现在可以 self-size 使用自动布局并忽略 return 在 collectionView(_:layout:sizeForItemAt:)
中的内容。如果要使用 collectionView(_:layout:sizeForItemAt:)
,请在故事板中将 Estimate size
更改为 None
。