Search code examples
iosswiftuicollectionviewuitabbarcontroller

UICollectionView in second tab causing UICollectionView in first tab to crash app


I am a beginner currently working on my first IOS app. In my apps home page, I have a top menu bar that consists of a horizontal collection view with 3 cell pages (3 tabs). Inside each of these cells are 3 collection views displaying objects from my database. I also have a UITabBarController with 3 tabs at the bottom.

I am trying to add another collection view in the second tab. However, as soon as I change the second tabs view controller class to of type uiCollectionViewController, the app will crash when I tab back to the first (home page) and try scrolling with my horizontal collection view. The bug only comes when the second tab is changed to of type UICollectionView.

I apologize if this hard to understand. I have been stuck on this for a while, so I had to try.

Thank you!!

Here is a photo of the error and my code when it crashes:

enter image description here


Solution

  • I finally figured out what the issue was here..

    I was providing the same layout parameter to two different collection views when setting them up as items in my tab bar controller.

    Let layout1 = UICollectionViewFlowLayout()
    
    Let firstController = FirstCollectionViewController(collectionViewLayout: layout1)
    
    Let layout2 = UICollectionViewFlowLayout()
    
    Let secondController = 
    SecondCollectionViewController(collectionViewLayout: layout2)
    

    This implementation fixed my issue!