0
votes

I have a vertical flow collectionview with everything constrained in auto layouy except the cell size.

the collectionview is constrained to the sides of the screen, the images and text are constrained within the custom cell. but i can't seem to constrain the cell to the collection view so when i change device size the cell stays the same size as the screen it was created on.

any way around this?

1

1 Answers

1
votes

Changing the size of the cell should be determined by implementing collectionView(_:layout:sizeForItemAt:):

Asks the delegate for the size of the specified item’s cell.

Obvoisly, your ViewController should conforms to UICollectionViewDelegateFlowLayout.

So, what can you do -for example-:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenSize: CGRect = UIScreen.main.bounds
    return CGSize(width: screenSize.width / 2, height: screenSize.height / 2)
}

This should make the size of the cell as: it's with is equals to the half of the screen size and the height is equals to the half of the screen size, regardless what' the device...

Hope this helped.