I have ViewController
where have two collectionView
but for one I want isPagingEnabled
for cell and for anther collectionView
3 item for full frame width. How can I do that ?
MenuCollectionView for paging : it working perfectly
func setupMenuCollection(){
if let flowLayout = menuCollectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLineSpacing = 0
}
menuCollectionView?.backgroundColor = UIColor.white
menuCollectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
menuCollectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0)
menuCollectionView?.isPagingEnabled = true
}
It is for manubarCollectionView : it is not working because here no else statement.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if manubarCollectionView == collectionView {
return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
}
}
It's simple, for second collectionView
cell size either add else block
and return CGSize
or directly return CGSize
after if block
.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if manubarCollectionView == collectionView {
return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
}
else {
//return cell size for menuCollectionView
return collectionView.frame.size
}
}
OR
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if manubarCollectionView == collectionView {
return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
}
//return cell size for menuCollectionView
return collectionView.frame.size
}
Note: You need to put this type of condition in all dataSource
and delegate
methods of collectionView
to differentiate two collectionView
.
See Cristan ,
menuCollectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
above method is used for setting space between two cell.
And below method collectionViewLayout is used for setting cell Width and Height
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if manubarCollectionView == collectionView {
return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
}
else
{
return CGSize(width: 100, height: 100)
}
}
If you want to handle different cell for two collection view then check
how to use two CollectionView on same view controller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With