0
Bu UICollectionViewCell dynamic sizing for 4.4 and 5.5 inch

Dinamik UICollectionViewCell genişlik ve en boy oranı

bir takip soru bu çözüm büyük olsa da, I (kCellsPerRow) et hücre dinamik miktarda üretmek için istiyorum olan

UIViewController'ın içinde çerçeveye Cihazın genişliğini, en boy oranını ve göstermem gereken toplam öğelerin miktarını temel alır. Sadece bir en boy oranı sınırına ulaştığında çok yeniden boyutlandırılmamaları için doğru miktarda kCellsPerRow değerini nasıl hesaplayacağımı anlayamıyorum.

İşte ben bugüne kadar ne var:

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) 

cevap

4

belirttiğiniz gereken bir hücre olması gereken en küçük boyutudur. Hücreler, en azından bu boyut kadar geniş (ve yüksek) ve muhtemelen daha büyük olacaktır.

yaklaşım: 1 en boy oranına:

olmayan bir 1 Taşıma

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

: Burada

kendi CollectionView'ın genişliğine göre, uygun bir hücre boyutuna hesaplayan bir yöntemidir

Görüntü oranınız 1: 1'den farklıysa, hesaplanan genişliği en boy oranına bölerek dinamik yüksekliği belirleyebilirsiniz. lu.

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

öğe boyutunu ayarlama: Biz ancak o zaman, bir hücre için genişliğini hesaplamak genişlik ve yükseklik hem de bu değeri kullanmak gerekir, 1: boy oranı yana

1'dir.

Seti hesaplanan dinamik boyutuna göre asıl öğe boyutu:

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

örnek sonuçlar:

Bu çeşitli konteyner genişliğine dayanan sana cep olacağını boyutunu bir fikir verecektir :

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