如何解决努力使集合视图高度适合内容
我有一些特殊情况,无论我找到什么 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)
}
现在,我想确保集合视图的高度适合内容,但这是我努力实现的地方。
我的集合视图位于堆栈视图内,所有约束都设置在堆栈视图的边缘。
我需要做什么来设置我的集合视图的高度以适应内容?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。