UICollectionView must be initialized with a non-nil layout parameter [Solved]

 

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

Initialization methods such as two parameters are not used in uicollectionview

Solution

  collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height),collectionViewLayout: flowLayout)

The supplement is all the source code in init Code:

    override init(frame: CGRect) {
        flowLayout = UICollectionViewFlowLayout()
        flowLayout.headerReferenceSize = .zero
        flowLayout.footerReferenceSize = .zero
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height),collectionViewLayout: flowLayout)
        pageControl = UIPageControl()
        super.init(frame:frame)
        self.setupViews()
    }

Similar Posts: