2016-04-13 28 views
0

aralığına getirin'u'u UICollectionView'da yatay akış düzeni için nasıl edinebilirim? Alabildiğim tek bilgi (ve set) minimumInteritemSpacing. flowLayout.itemSize = CGSizeMake((_collectionView.frame.size.width/3.0)-(flowLayout.minimumInteritemSpacing/3.0), _collectionView.frame.size.height); I:gerçek UICollectionView öğesini

ne olursa olsun ekran boyutu (yani formül interItemSpacing geçerli değeri minimumInteritemSpacing değiştirmek isteyen) toplama görünümünde her ekran 3 eşit genişlik öğeleri, aşağıdaki küçük hesaplama uğruna mi Bu hesaplamayı viewDidLayoutSubviews içine koyun ve geçerli ekranlar için tam olarak çalışmıyor çünkü geçerli interItemSpacing, minimumInteritemSpacing

şimdiden teşekkürler. Bu deneyin sabitleme aralıklı hücreler oluşturmak için

cevap

1

Daha fazla bilgi için

private let minItemSpacing: CGFloat = 8 
private let itemWidth: CGFloat  = 100 
private let headerHeight: CGFloat = 32 

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    // Create our custom flow layout that evenly space out the items, and have them in the center 
    let layout = UICollectionViewFlowLayout() 
    layout.itemSize = CGSize(width: itemWidth, height: itemWidth) 
    layout.minimumInteritemSpacing = minItemSpacing 
    layout.minimumLineSpacing = minItemSpacing 
    layout.headerReferenceSize = CGSize(width: 0, height: headerHeight) 

    // Find n, where n is the number of item that can fit into the collection view 
    var n: CGFloat = 1 
    let containerWidth = collectionView.bounds.width 
    while true { 
     let nextN = n + 1 
     let totalWidth = (nextN*itemWidth) + (nextN-1)*minItemSpacing 
     if totalWidth > containerWidth { 
      break 
     } else { 
      n = nextN 
     } 
    } 

    // Calculate the section inset for left and right. 
    // Setting this section inset will manipulate the items such that they will all be aligned horizontally center. 
    let inset = max(minItemSpacing, floor((containerWidth - (n*itemWidth) - (n-1)*minItemSpacing)/2)) 
    layout.sectionInset = UIEdgeInsets(top: minItemSpacing, left: inset, bottom: minItemSpacing, right: inset) 

    collectionView.collectionViewLayout = layout 
} 

, bu mükemmel makale:

http://samwize.com/2015/11/30/understanding-uicollection-flow-layout/