0

I want to use collection view for show user's instagram photos like Tinder. I placed a collectionview and enabled paging. But i don't know how to show 6 photos for each page. How can i do this?

This is what i want:

First page:

enter image description here

Second Page:

enter image description here

I used all spacing settings zero because setting spacing from storyboard problem for different screen sizes.

My collection view's settings and result:

enter image description here

Result:

enter image description here

So i want to show 6 photos per page. How to achive this?

Tolgay Toklar
  • 4,151
  • 8
  • 43
  • 73
  • I think you'll find a good solution here: https://stackoverflow.com/questions/16678474/uicollectionview-horizontal-paging-can-i-use-flow-layout – DonMag Aug 08 '17 at 15:36

1 Answers1

0

To show the specific number of cells you want, you have to calculate the size of the cells. Something like this:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let numberOfRows = 2
    let numberOfColumns = 3

    let height = collectionView.frame.size.height / numberOfRows
    let width = collectionView.frame.size.width / numberOfColumns

    let cellSize = CGSize(width: width, height: height)

    return cellSize
}

Since your layout configuration is static you can calculate it just once to make it faster:

class MyVC: UIViewController {

    private let cellSize: CGSize = CGSize(width: collectionView.frame.width/numberOfColumns, height: collectionView.frame.height/numberOfRows) // or create a computed property

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

This code is untested, maybe you have to add some padding but you get the idea. Another option would be to implement your own UICollectionViewLayout subclass

henrique
  • 1,072
  • 10
  • 17