Dies ist ein Follow-up ist Frage von UICollectionViewCell dynamic sizing for 4.4 and 5.5 inchDynamische UICollectionViewCell Breite bezüglich Breite und Seitenverhältnis zu gestalten
Während diese Lösung groß ist, ich möchte eine dynamische Menge Artikel-Zellen (kCellsPerRow) innerhalb des UIViewController erzeugen basierend auf der Breite des Geräts, dem Seitenverhältnis und der Anzahl der insgesamt angezeigten Elemente. Ich kann einfach nicht herausfinden, wie man die richtige Menge von kCellsPerRow berechnet, so dass sie nicht zu groß werden, sobald sie ein Seitenverhältnis-Limit erreicht haben.
Hier ist, was ich bisher:
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)