UICollectionView Layout Error: nil layout parameter

Hey guys, when I run my project the error: 'UICollectionView must be initialized with a non-nil layout parameter', appears. I don't know why because I already used the same code in another project and it worked.
This is my code:

Code Block
 var myCollectionView : UICollectionView?
func setUpContactsCollectionView() {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 100, height: 100)
        layout.scrollDirection = .horizontal
      
        myCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        myCollectionView?.showsHorizontalScrollIndicator = false
        
        myCollectionView?.register(InserateCollectionCell.self, forCellWithReuseIdentifier: "InserateCollectionCell")
        myCollectionView?.backgroundColor = UIColor.white
        
        myCollectionView?.dataSource = self
        myCollectionView?.delegate = self
 
myCollectionView?.translatesAutoresizingMaskIntoConstraints = false
        myCollectionView?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        myCollectionView?.topAnchor.constraint(equalTo: topAnchor, constant: 50).isActive = true
        myCollectionView?.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        myCollectionView?.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
    }

Thanks in advance!
Your code lacks many parts and I cannot run your code.
Can you show a complete code I can run and test easily?
Could you show where exactly you get the error ?

Also change for non zero frame, that you will adjust later:

Code Block
        myCollectionView = UICollectionView(frame: CGRect(x: 20, y: 20, width: 100, height: 100), collectionViewLayout: layout)

You haven't shown where centerXAnchor (or the other Anchors) come from.
Is it possible that one of them is nil?
@robnotyou I did that because the collectionView is in a tableView Cell, so I don't use the typical view.Anchor phrases...
@Chris0309
Which line exactly (in your OP) do you get the error ?

Did you try setting the frame ?
@Claude31
Well, the error appears in the App Delegate

Yes I settled the frame but it also not worked :(
@OOPer
The collectionView is placed in a tableView Cell, that's the reason why the code will lack if you run it as normal CollectionView

the error appears in the App Delegate

Do you mean that's where the crash shows ?
That's not the initial cause, could you look at the crash report ?
@Claude31
Exception NSException * "UICollectionView must be initialized with a non-nil layout parameter" 0x0000000282926a90

Is this what you need?
In the crash report, can you follow the track to see where exactly ? Is it inside setUpContactsCollectionView ?
You can also track step by step in debugger to see where it crashes.
Or add several prints in setUpContactsCollectionView, to see which is the last to print.

Could you also show the full class, to understand better the context.
Well @Claude31 and to all the others, thank you for your effort and help,
I'm going to delete the Code and try it new in a different way because there is definitely something really weird. I deactivated the whole CollectionView Code and it even showed me exactly the same error....

Maybe it's too late with the answer, but I found a solution. I had the same problem myself. It seems that the code is identical, but small details can lead to an error

lazy var collectionView: UICollectionView = {

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: CollectionViewLayout().createCompositionaLayout())

        collectionView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)

        collectionView.register(PreviewCardPersonCollectionViewCell.self, forCellWithReuseIdentifier: "PreviewCardPersonCollectionViewCell")

        collectionView.translatesAutoresizingMaskIntoConstraints = false

        collectionView.dataSource = self

        collectionView.delegate = self

        collectionView.backgroundColor = .red

        return collectionView

    }()

self.addSubview(collectionView)

        collectionView(equalTo: self.leadingAnchor, constant: 20).isActive = true

        collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true

        collectionView.topAnchor.constraint(equalTo: self.topAnchor, constant: 20).isActive = true

        collectionView.heightAnchor.constraint(equalToConstant: 100).isActive = true
UICollectionView Layout Error: nil layout parameter
 
 
Q