user2924482
user2924482

Reputation: 9140

iOS:Setting UICollectionView cell to view size

I'm trying to figure out how to setup the frame of the UICollectionView cell to the frame of the view (similar of how photo app) but I need to do it programmatically. Any of you knows how can I do this?

I'll really appreciate your help.

Upvotes: 0

Views: 145

Answers (1)

nathangitter
nathangitter

Reputation: 9797

You can use the sizeForItemAt indexPath method on UICollectionViewDelegateFlowLayout. This will determine the size of each cell, and can be configured to be relative to the size of other elements (like your view, for example).

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width / 2, height: 50)
}

Just make sure the deleagte of your collection view is set, and the delegate conforms to the UICollectionViewDelegateFlowLayout protocol.

Upvotes: 1

Related Questions