0

これはフォローアップ質問ある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) 

答えて

4

指定する必要があるすべてのセルがあるべき最小サイズです。細胞は、少なくともそのサイズと同じ幅(そして高い)であり、場合によってはそれより大きい。

アプローチ:1つのアスペクト比:

非1の処理

// 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 
} 

:ここ

はcollectionViewの幅に基づいて適切なセルサイズを計算する方法です

アスペクト比が1:1と異なる場合は、計算された幅をアスペクト比raで除算してダイナミックハイトを決定するだけですtio。お使いのアスペクト比が1であるので

::アイテムのサイズを設定する

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

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