Reputation: 11
I have 3 different UICollectionView
s in the same UIViewController
. They are working fine. My problem is with the styling, 2 of them need to have the same styling - meaning 3 columns and some spacing.
This is what i need to achieve: screenshot
And this is what I got so far: desired result
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == gameCollectionView {
let width = gameCollectionView.frame.size.width
return CGSize(width: ((width / 2) - 7), height: 150)
}
else {
let width = paymentCollectionView.bounds.width / 3 // < THIS IS What should give 3 columns
return CGSize(width: width, height: 70)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
Can someone tell me how to achieve this and preferably explain how it works because I cannot figure it out
UPDATE: As requested - Storyboard for the CollectionView Storyboard
Upvotes: 0
Views: 1899
Reputation: 81
CGSize(width: ((width - `section's left & right spce` - minimumInteritemSpacing) / `numberofCellYouWant`), height: 150)
you can get minimumInteritemSpacing
from collectionView.collectionViewLayout
property
Upvotes: 4
Reputation: 31
If you're planning to deploy for iOS 13 or above I'd recommend using Compositional Layout to achieve this grid.
Apple has a great WWDC on this and an SDK that you can download to view their sample code. Here are the links:
Advances in Collection View Layout WWDC 2019
I'd also recommend wrapping all three collection views inside this single collection view. This can easily be achieved using Compositional Layout.
The sample below is from the link above. You can layout 3 columns by simply defining it in your group. Alternatively you could layout your items as a fraction of their group container.
func createLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 3)
let spacing = CGFloat(10)
group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = spacing
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
}
Upvotes: 3
Reputation: 78
You need to change this delegate work
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == gameCollectionView {
let width = gameCollectionView.frame.size.width
return CGSize(width: ((width / 2) - 7), height: 150)
}
else {
let width = (paymentCollectionView.frame.width / 3) - 5 // if you want to more space so increase
return CGSize(width: width, height: 70)
}
}
Upvotes: 1
Reputation: 5693
You can try this
let screenWidth = self.CollecionView.frame.width
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: (screenWidth/3)-5, height: (screenWidth/3)-5)
self.CollecionView.collectionViewLayout = layout
Upvotes: 0