Search code examples
iosswiftuicollectionviewuicollectionviewflowlayout

UICollectionView with two UICollectionViewFlowLayout


I'm working on a project in iOS, Swift. How Can we assign one collectionView with two different flow layout? Here I need my UICollectionViewCell to look like a stack-card, for that, I'm using CardsCollectionViewLayout(external pod file).

myCollectionView.collectionViewLayout = CardsCollectionViewLayout()

And it is working fine. But the issue is I cant adjust the UICollectionViewCell width with device width. For that, I need to use UICollectionViewDelegateFlowLayout.

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let nCol = 1
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let totalSpace = flowLayout.sectionInset.left
        + flowLayout.sectionInset.right
        + (flowLayout.minimumInteritemSpacing * CGFloat(nbCol - 1))
    let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(nbCol))

    return CGSize(width: size, height: 284)
}

But When I use CardsCollectionViewLayout as my collectionView layout, I did not get the call back in the above method.

OR

Is there any way to add stack-card effect with my UICollectionViewCell.

enter image description here

Please help me.


Solution

  • You are looking it from a wrong prospective, if you use CardsCollectionViewLayout you have to use the function provided by that layout in order to change it. There's a property called itemSize in CardsCollectionViewLayout, which you can set to define the size of the cards:

    let layout = CardsCollectionViewLayout()
    collectionView.collectionViewLayout = layout
    layout.itemSize = CGSize(width: YOUR_WIDTH, height: YOUR_HEIGHT)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.isPagingEnabled = true
    collectionView.showsHorizontalScrollIndicator = false