0

I am working on an iOS app in swift 4. I have implemented collection view and there are two cells at the front as shown in the image: CollectionViewCells-Image

When I run my app on bigger screen sizes like iPhone 6/7/8 Plus, then it Shows two complete cells with one-half cell. What I want to achieve is that it shows two cells in front of every screen size, Please help.

Dixit Akabari
  • 1,959
  • 9
  • 21
JenniSole
  • 51
  • 3
  • 1
    Look for `optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize` https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617708-collectionview – Larme Feb 15 '18 at 13:12
  • 3
    Possible duplicate of [How to set UICollectionViewCell Width and Height programmatically](https://stackoverflow.com/questions/38028013/how-to-set-uicollectionviewcell-width-and-height-programmatically) – Larme Feb 15 '18 at 13:12
  • try this : func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.frame.size.width , height:collectionView.frame.size.height/2) } – Dixit Akabari Feb 15 '18 at 13:17
  • Tried this code but no success. Everything stays same. – JenniSole Feb 15 '18 at 13:39

1 Answers1

0

Implement the UICollectionViewDelegateFlowLayout protocol in your ViewController, then implement:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellSize = //calculate cell size based on the width of the view and padding between cells
    return cellSize
}

Check out the documentation for the UICollectionViewDelegateFlowLayout and implement its methods to modify the spacing between the cells.

LorenzOliveto
  • 6,809
  • 1
  • 16
  • 44