Search code examples
iosobjective-cswiftuicollectionviewuicollectionviewlayout

Implement two CollectionView Layouts in one UICollectionView


Someone already asked this here (Can we set different flowlayout in different sections within a UICollectionView?)..... but no one has responded and I have too little reputation to comment and bump the post.

I'm using a collectionView to sort of emulate a tableView, with the first few cells stretching across the width of the screen. However I'm hoping to create a second section and have the layout change into more of a waterfall effect, similar to Pinterest. I have the layout for both sections already made, I'm just wondering if anyone has any ideas on how to implement two layouts at the same time.

Can this be done?

Thanks


Solution

  • You would create two collection views and assign FirstCollectionViewFlowLayout() to your first collectionView, and SecondCollectionViewFlowLayout() to the second.

    let firstFlowLayout = FirstCollectionViewFlowLayout()
    let firstCollectionView = UICollectionView(frame: self.view.frame, 
            collectionViewLayout: firstFlowLayout)
    // and 
    let secondFlowLayout = SecondCollectionViewFlowLayout()
    let secondCollectionView = UICollectionView(frame: self.view.frame, 
            collectionViewLayout: secondFlowLayout)
    

    Then if you have any differences in content, you would probably need some conditional in other functions, it would depend on you further implementation.