Trinity
Trinity

Reputation: 15

UICollectionViewCell not filling screen width

I am trying to make my UICollectionView's cells fill the whole width, but it just looks like this:

enter image description here

I tried something like this:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height+140)
    }

But it doesn't do anything. Tips?

Upvotes: 1

Views: 1262

Answers (1)

Omer Tekbiyik
Omer Tekbiyik

Reputation: 4744

You need to use sizeForItemAt instead of sizeForItemAtIndexPath. sizeForItemAtIndexPath Asks the delegate for the size of the specified item’s cell.

You need to Use Swift 3+:

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

Upvotes: 1

Related Questions