Want to create square shape cells in collectionview with based on my count index iOS swift
Problem:
I want to create a collectionview cell based on my count of single row then i want to arrange the entire collectionview.How to achieve that?
Solution:
The below code was shown 3 items in a row with space between items is 8.
class myVC: UIViewController {
let itemsInRow: CGFloat = 3
let ItemSpace: CGFloat = 8
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let totalSpace = ItemSpace * itemsInRow
let availableWidth = collectionView.frame.width - totalSpace
let itemWidth = availableWidth / itemsInRow
return CGSize(width: itemWidth, height: 20)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return ItemSpace
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return ItemSpace
}
}
Comments
Post a Comment