ios - 努力使集合视图高度适合内容
问题描述
我有一些特殊情况,无论我找到什么 StackOverflow 答案,它似乎都对我不起作用。我有一个包含动态单元格数量的集合视图(2-3 个单元格之间的任意位置)。但是,无论有多少单元格,此集合视图的高度将始终相同。
这是我设置单元格高度的逻辑:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// The cell's width and height, to be calculated
var cellWidth: CGFloat = 0.0, cellHeight: CGFloat = 0.0
// The number of cells to show, 2-3
let count = numberOfCellsToShow
// The width of the collection view
let collectionViewWidth = collectionView.bounds.size.width
// Calculate the maximum height for collection view (16:9 ratio)
let collectionViewMaxHeight = collectionViewWidth / (16 / 9)
// Get the width and height of each cell, with 4dp of spacing between cells
if (count == 2) {
cellWidth = (collectionViewWidth - CGFloat(4)) / 2
cellHeight = collectionViewMaxHeight
} else if (count == 3) {
cellWidth = (collectionViewWidth - (CGFloat(4) * 2)) / 3
cellHeight = collectionViewMaxHeight
}
return CGSize(width: cellWidth, height: cellHeight)
}
现在,我想确保集合视图的高度适合内容,但这是我努力实现它的地方。
我的集合视图位于堆栈视图内,所有约束都设置在堆栈视图的边缘。
我需要做什么来设置我的收藏视图的高度以适应内容?
解决方案
推荐阅读
- svelte - 在 svelte todo 应用程序中更新 todo 时出现问题。使用“商店”
- .htaccess - 如何总是在 https://www. 处重定向?
- typescript - 如何重用包装了更新其内容的 Viewchild 的 ngTemplate
- c++ - 关于在 Windows 上使用 g++ 和代码块编译 zlib 示例项目的问题
- python-3.x - 很少有记录写入谷歌表
- php - 如何在运行时获取 Android 应用程序的 OAuth 客户端密钥和客户端密钥而无需硬编码
- asp.net-core - Blazor checkbox binding is not working - server-side
- javascript - 每次在 ReactJS 中更改时获取单选按钮值
- html - 在 Angular 中声明 ImageObject 格式在哪里?
- python - 有没有办法在 manim 中创建散点图?