How to set the collection view cell size exactly equal to the collection view in iOS?

后端 未结 3 1950
梦毁少年i
梦毁少年i 2021-01-20 01:01

I want to set a collection view where it is only displayed one element at the time and it scrolls horizontally. I want to know how to set the same size for both if the colle

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 01:55

    Simple answer :

    Add UICollectionViewDelegateFlowLayout to your class and then use this method:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: yourCollectionView.bounds.width, height: yourCollectionView.bounds.height)
    }
    

    to make it horizontal:

提交回复
热议问题