gpt4 book ai didi

swift - 相对于框架宽度和纵横比的动态 UICollectionViewCell 宽度

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

这是来自 UICollectionViewCell dynamic sizing for 4.4 and 5.5 inch 的后续问题

虽然该解决方案很棒,但我想根据设备的宽度、纵横比和我必须显示的项目总数在 UIVIewController 中生成动态数量的项目单元格 (kCellsPerRow)。我只是想不通如何计算正确数量的 kCellsPerRow,以便它们在达到纵横比限制后不会过度调整大小。

这是我目前所拥有的:

var kCellsPerRow = ceil(global.TotalAmount)                //Total Amount of Cells
var windowWidth = layout.collectionView!.frame.width // Device's Window Width
var dynamicWidth = windowWidth / CGFloat(kCellsPerRow) // Current Width of Each Cell
var dynamicHeight = windowWidth / CGFloat(kCellsPerRow) // Current Height of Each Cell
var limit = (Double(dynamicWidth) / Double(windowWidth))*(9/2) < (2/9) // Constraint/Threshold

if ( limit ) {
var newkCellsPerRow = CGFloat(kCellsPerRow * (2/9)) // Attempt
newkCellsPerRow = 9 // Fixed Size of 9 but what if we can dynamically figure out the perfect number such that its not so tiny!

dynamicWidth = layout.collectionView!.frame.width / newkCellsPerRow
dynamicHeight = layout.collectionView!.frame.width / newkCellsPerRow
}

// Create Cell
layout.itemSize = CGSize(width: dynamicWidth, height: dynamicHeight)

最佳答案

您只需要指定单元格的最小尺寸即可。细胞将至少与该尺寸一样宽(和高),并且可能更大。

方法:

这里有一个方法可以根据其 collectionView 的宽度计算适当的单元格大小:

// These variables are set in the Storyboard, but shown here for clarity
flowLayout.minimumInteritemSpacing = 10
flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

let minimumSize: CGFloat = 90.0 // A cell's width or height won't ever be smaller than this

func cellWidthForViewWidth(viewWidth: CGFloat) -> CGFloat {
// Determine the largest number of cells that could possibly fit in a row, based on the cell's minimum size
var numberOfCellsPerRow = Int(viewWidth / minimumSize)

// Adjust for interitem spacing and section insets
let availableWidth = viewWidth - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing * CGFloat(numberOfCellsPerRow - 1)
numberOfCellsPerRow = Int(availableWidth / minimumSize)

return availableWidth / CGFloat(numberOfCellsPerRow) // Make this an integral width if desired
}

处理非 1:1 纵横比:

如果您的纵横比恰好不是 1:1,您只需将计算出的宽度除以纵横比即可确定动态高度。

let aspectRatio: CGFloat = 3.0/2.0
let cellWidth = cellWidthForViewWidth(CGRectGetWidth(collectionView.frame))
let cellHeight = cellWidth / aspectRatio

设置项目大小:

由于您的宽高比是 1:1,我们只需要计算单元格的宽度,然后使用该值作为宽度和高度。

根据计算出的动态尺寸设置实际项目尺寸:

let actualCellSize = cellWidthForViewWidth(CGRectGetWidth(collectionView.frame))
flowLayout.itemSize = CGSize(width: actualCellSize, height: actualCellSize)

示例结果:

根据不同的容器宽度,这会让您了解单元格的大小:

print(cellWidthForViewWidth(320.0))  //  93,  3 cells per row
print(cellWidthForViewWidth(400.0)) // 116, 3 cells per row
print(cellWidthForViewWidth(640.0)) // 93, 6 cells per row
print(cellWidthForViewWidth(800.0)) // 101, 7 cells per row
print(cellWidthForViewWidth(1024.0)) // 90, 10 cells per row

关于swift - 相对于框架宽度和纵横比的动态 UICollectionViewCell 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312979/

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