2017-10-11 3 views
2

Ich bin ein Anfänger in der iOS-Entwicklung, und ich befolge ein Tutorial zur Implementierung der Sammlungsansicht. HierUICollectionView item spacing kann nicht angepasst werden

ist der Screenshot dieser App:

as we can see, the space between cell and line is quite big

Ich möchte diese Lücke zwischen den Elementen so nah wie möglich machen. Ich habe versucht, unter dem Code zu implementieren:

minimimum spacing in storyboard

aber die Lücke ist nach wie vor groß:

struct StoryBoard { 
    static let leftAndRightPaddings : CGFloat = 1.0 
    static let numberOfItemsPerRow : CGFloat = 3.0 
} 

let collectionViewWidth = collectionView?.frame.width 
let itemWidth = (collectionViewWidth! - StoryBoard.leftAndRightPaddings)/StoryBoard.numberOfItemsPerRow 

let layout = collectionViewLayout as! UICollectionViewFlowLayout 
layout.itemSize = CGSize(width: itemWidth, height: itemWidth) 

Ich habe auch den Mindestabstand für die Zell- und Linie zu 0,1 in Hauptstoryboard wie folgt geändert . Was ist hier falsch gelaufen?

+0

kann diese Ihnen helfen. https://stackoverflow.com/questions/35654129/how-to-change-space-between-cells-in-uicollectionview/35654505#35654505 –

+0

Wird das ImageView auf jeder Zelle gefüllt? Sie können die Debug-Ansichtshierarchie verwenden, um sie zu überprüfen – jojoT

Antwort

3

Sie können durch die Verwendung von UICollectionViewDelegateFlowLayout Methode

Für EX verwalten.

Verwenden Sie die folgenden Verfahren verwalten für

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let width = UIScreen.main.bounds.size.width 
     var height = 124.0 // Set whatever you want 
     return CGSize(width: (width/3) - 0.5, height: CGFloat(height)) // Here (width/3) means 3 cell display in screen horizontally. you can change here 3 or 2 or 4 etc AND "0.5" is space your can change space between two items that you want. 
} 
0

Sie haben die Objektgröße korrekt zu berechnen, wie auch 1 Pixel gestört Layout führen kann. Kleine Änderung für @iPatel antworten

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let noOfItemsInRow = 3 // the items in single row 
    let width = UIScreen.main.bounds.size.width 
    let totalSpace = 8 + 8 + (noOfItemsInRow * 0.5) //8: side margin, 0.5 is the space between 2 items 
    var height = 124.0 // Set whatever you want 
    return CGSize(width: ((width - totalSpace)/3), height: CGFloat(height)) // Here (width/3) means 3 cell display in screen horizontally. you can change here 3 or 2 or 4 etc 

}

Hoffnung diese Hilfe

Verwandte Themen