CollectionView sizeForItemAtIndexPath never called

前端 未结 15 1481
一整个雨季
一整个雨季 2020-12-24 11:17

This is driving me crazy! I have a UICollectionViewController as shown below:

class PhrasesCompactCollectionViewController: UICollectionViewController
         


        
15条回答
  •  余生分开走
    2020-12-24 11:47

    First class need confirm to UICollectionViewDelegateFlowLayout. Then you need to write following code in viewDidLoad():

    //To tell the UICollectionView to use your UIViewController's UICollectionViewDelegateFlowLayout methods

    collectionView.delegate = self
    

    // If you want to set your own collection view flow layout

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //depending upon direction of collection view
    
    self.collectionView?.setCollectionViewLayout(layout, animated: true)
    

    By using this code UICollectionViewDelegateFlowLayout method

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    

    will get called, you can set size in this method.

提交回复
热议问题