-2

I'm working in Swift and I want my cells to look similar to how it does on this picture on an iPhone 8 plus enter image description here

But when I load it on a iPad, the size of the cells do not adjust to the screen size. It looks like this: enter image description here

Does anyone know how I can do this in my CollectionViewController?

Tessa
  • 403
  • 1
  • 5
  • 13

2 Answers2

1

Try this.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let yourWidth = (collectionView.bounds.width) / 10.0
    let yourHeight = yourWidth

    return CGSize(width: yourWidth, height: yourHeight)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0, 0, 0, 0)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
0

There is a delegate Method in UICollectionViewDelegateFlowLayout

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


    let collectionViewCellSize = collectionView.frame.size.width / 10 // to show 10 cells in a row

    return CGSize(width: collectionViewCellSize, height: collectionViewCellSize)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0, 0, 0, 0)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
Taimoor Suleman
  • 1,588
  • 14
  • 29