これはフォローアップ質問あるUICollectionViewCell dynamic sizing for 4.4 and 5.5 inch動的UICollectionViewCell幅
からその溶液が大きい一方で、私はの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)