// install pod :: AlignedCollectionViewFlowLayout
// also assign class AlignedCollectionViewFlowLayout in storyboard
func setLayout() {
let alignedFlowLayout = AlignedCollectionViewFlowLayout(horizontalAlignment: .justified, verticalAlignment: .center)
statsCollectionView.collectionViewLayout = alignedFlowLayout
statsCollectionView.delegate = self
statsCollectionView.dataSource = self
}
}
// MARK: - Collection View
extension GameStatsViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
print("sec :", section)
return UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0)//here your custom value for spacing
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var widthPerItem : CGFloat = 0
var heightPerItem : CGFloat = 0
widthPerItem = collectionView.frame.width / 3 - 7
heightPerItem = widthPerItem + 5
return CGSize(width:widthPerItem, height: heightPerItem)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return namesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = statsCollectionView.dequeueReusableCell(withReuseIdentifier: "GameStatsCollectionViewCell", for: indexPath) as! GameStatsCollectionViewCell
cell.name.text = namesArray[indexPath.row]
cell.points.text = numbers[indexPath.row]
return cell
}
}
Comments