4

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)
        }

    }
Nirav D
  • 71,513
  • 12
  • 161
  • 183
cristan lika
  • 415
  • 1
  • 7
  • 22

2 Answers2

7

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.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
1

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

Community
  • 1
  • 1
Vivek Gajbe
  • 411
  • 2
  • 14