UICollectionView: must be initialized with a non-nil layout parameter

后端 未结 7 2206
不思量自难忘°
不思量自难忘° 2020-11-27 05:25

I added UICollectionView by code.
Now, the app crash with message: UICollectionView must be initialized with a non-nil layout parameter.
Do

7条回答
  •  醉话见心
    2020-11-27 06:07

    Just a swift version of Riley Avron's answer

        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(100, 100)
        let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        self.view.addSubview(collectionView)
        collectionView.delegate   = self
        collectionView.dataSource = self
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")
    

提交回复
热议问题