1

I have an UICollecitonView. I need that my cell size to be equal as UICollectionView Frame size because of the paging, and I handled the spacing by trailing and leading space in UICollectionViewCell's content view, and the spacing of UICollectionView is actually zero. and I set the clipToBounds of UICollectionView to false. When I use Debug View Hierarchy, the UICollectionView has two visible cells at the same time. But in my case I need at least 3 visible cells at the same time, Because I need to show the edge of previous cell and the next cell, like what you see in the image. enter image description here

How can I do that?

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return collectionView.frame.size 
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.data_array.count
}
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
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
NabiMo
  • 105
  • 9
  • First you need to provide more information about how you are creating your view. How are you specifying the size of the cells currently? And the spacing. Show some code. The more information you provide the more detailed and relevant the answers will be. – Fogmeister Sep 27 '18 at 08:38
  • @Fogmeister I've edited the question – NabiMo Sep 27 '18 at 08:56

1 Answers1

1

Currently the size of the cell that you are returning is the size of the collection view.

So there will only ever be one full cell on screen.

If you want to show the edges of the other cells then return a smaller size in the size for item function.

Something like...

CGSize(width: collectionView.bounds.size.width - 40, height: collectionView.bounds.size.height)

That would make the cell smaller and should show some edges.

You will have to play around with different sizes and item spacing to perfect it.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • I need that the cell always be in the center of UICollectionView, And if I don't set the width as same as collectionView's width, it won't happen. when I set clipToBounds to false, it shows other cells those are outside of collectionView frame. but the problem is it always shows the next cell and not previous cell. I need the both edges to be shown. – NabiMo Sep 27 '18 at 09:16
  • If you want it to always be in the centre of the screen then there is a much better way. First... do what I said in my answer. Now your first cell will be left aligned, so update the content inset `.left` to make the first cell centred. Then use the function described here... https://stackoverflow.com/questions/13492037/targetcontentoffsetforproposedcontentoffsetwithscrollingvelocity-without-subcla to then make sure the scrolling always stops with the cell in the centre. You'll have the same problem with the last cell so update the content inset to make that one centred too. – Fogmeister Sep 27 '18 at 09:40