相对于框架宽度和纵横比的动态 UICollectionViewCell 宽度
Dynamic UICollectionViewCell width with respect to frame width and aspect ratio
这是 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
这是 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