2016-08-05 18 views
1

Ich versuche, alle Zellen in einem UICollectionView in der Mitte zu zentrieren. Die Zellen sind als 11x4 geteilt.Swift - Collection View - Center Zellen in der Mitte der Sammlung View

Ich habe versucht, mit dem Code unten, aber die Zellen sind in einer Zeile gruppiert.

Was ich versuche, ist in dem zweiten Bild gezeigt zu erreichen:

FALSCH enter image description here

SOLL BE enter image description here

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let letterCell = collectionView.dequeueReusableCellWithReuseIdentifier("letterCell", forIndexPath: indexPath) as! LetterCellView 

     letterCell.center.y = self.view.center.y 

     return letterCell 

    } 
in meinem Code

auch amüsant ich collectionViewLayout die Zellen zu teilen in 4 Zeilen, vielleicht sollte ich hier das Zentrierungssystem implementieren, aber ich bekomme nicht, wie ich das erreichen kann. Jede Idee ?:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 

     let totalCellWidth = 30 * 11 
     let totalSpacingWidth = 2 * (11 - 1) 

     let leftInset = (self.view.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth))/2; 
     let rightInset = leftInset 

     return UIEdgeInsetsMake(0, leftInset, 0, rightInset) 
    } 

Antwort

0

die Lösung gefunden out:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 

     let totalCellWidth = 30 * 11 
     let totalSpacingWidth = 2 * (11 - 1) 

     let leftInset = (self.view.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth))/2; 
     let rightInset = leftInset 

     let totalCellHeight = 30 * 4 
     let totalSpacingHeight = 5 * (4 - 1) 

     let topInset = (self.view.frame.size.height - CGFloat(totalCellHeight + totalSpacingHeight))/2; 
     let bottInset = topInset 

     return UIEdgeInsetsMake(topInset, leftInset, bottInset, rightInset) 
    } 
2

, wenn Sie ein visueller Ansatz bevorzugen versuchen, die Einsätze zu ändern if you prefer a more visual approach try changing the insets