0

Hello I am making horizontal collection view image on screen then scroll to view second the problem is when i give spacing to my collectionView then scrol to another images it show half image of another record mean half image one side and image of first record guide me how i give proper spacing i want to show single image on screen the scrol to view second with spacing this is screen shot of spacing

enter image description here

this is what I am facing when i scroll 1 or 2 images then it show me half image of another record enter image description here

I am already using CollectionViewLayout to give equal width and height to my cell

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let width = collectionView.frame.size.width
            let height = collectionView.frame.size.height
            return CGSize.init(width: width, height: height)

        }
Jhony
  • 187
  • 1
  • 4
  • 16

1 Answers1

0

To make UICollectionView perfectly, you need to:

  1. Add collectionView's delegate and data source properties
  2. You need to specify the size of the UICollectionView cell in storyboard or through code by implementing UICollectionViewDelegateFlowLayout methods.
  3. For spacing, you need to give the cell spacing.
  4. Provide the section insets in case you need it.

And you are good to go.

Edit

Here is the code which you can use to give the size and spacing details of UICollectionViewCell.

Also don't forget to conform to UICollectionViewDelegateFlowLayout protocol.

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

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
    return UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
}
halfer
  • 19,824
  • 17
  • 99
  • 186
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • it gives space between cell but still its showing half half cells of two reocrds i want to see one record in centered – Jhony Sep 17 '18 at 06:34
  • i want view something like that https://stackoverflow.com/questions/35045155/how-to-create-a-centered-uicollectionview-like-in-spotifys-player – Jhony Sep 17 '18 at 06:43
  • Check if the clip to bounds of `UIImageView` in selected. – PGDev Sep 17 '18 at 10:31
  • just select clip to bounds for uiimageview but still same issue :( – Jhony Sep 17 '18 at 11:53
  • Check if the above `UICollectionViewDelegateFlowLayout` methods are called. – PGDev Sep 17 '18 at 11:59
  • In that case you need to add some more code for me to debug the issue. – PGDev Sep 17 '18 at 12:37
  • bro i want exactly this type of collection view in this link code is in objective c please guide me to make like this https://github.com/raheelsadiq/UICollectionView-horizontal-paging-with-3-items – Jhony Sep 21 '18 at 06:09