1

I take UICollectionView and set horizontal scroll. Now In this one image view is there, All working perfect except cell spacing. In collectionView I set the Minimum Spacing for cells 0(Zero). But still Spaces is there.

My output is like that.. enter image description here

But I need this type of output..

enter image description here

And one more thing is that I refer so many answer but still no idea what to do. I use Xcode 7.0 so I need solution in swift 2.0 and if possible then also request to give answer in swift 3.0. Please help me. Thanks in advanced.

NiravS
  • 896
  • 1
  • 8
  • 22
  • 1
    Did you try it with code? check this: https://stackoverflow.com/questions/28325277/how-to-set-cell-spacing-and-uicollectionview-uicollectionviewflowlayout-size-r – Dharmesh Kheni Jul 29 '17 at 07:28
  • func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { return 0 } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: 155, height: 155) } I also put this method of UICollectionViewDelegateFlowLayout but still not working.. – NiravS Jul 29 '17 at 07:32
  • are u sure deleagte are getting called?? – Ramesh.G Jul 29 '17 at 07:32
  • Try in `viewDidLoad` shown in that link I gave. – Dharmesh Kheni Jul 29 '17 at 07:33
  • Yaa.. I put the break point into that.. So method called but not working. – NiravS Jul 29 '17 at 07:33
  • @Dhamesh I already try this code but when I put that code so my scrolling became vertically.. – NiravS Jul 29 '17 at 07:36

4 Answers4

3

try this

dont forget to set collectionview flowlayout delegate

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            return CGSize(width: collectionViewTable.bounds.size.width/2 - 20, height: 160)

         }

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return  UIEdgeInsetsMake( 5,  14, 5,  14)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 20;
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 20;
    }
Ramesh.G
  • 349
  • 1
  • 10
  • I refer so many code but all about vertically scroll. My scrolling is horizontally – NiravS Jul 29 '17 at 07:43
  • u have to change values and test i didnt tested as per your requirement can u show the screen shot? – Ramesh.G Jul 29 '17 at 07:43
  • Ya.. thats working but when I put code in viewDidload() with help of UICollectionViewFlowLayout So that time not working – NiravS Jul 29 '17 at 07:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150451/discussion-between-ramesh-and-niravs). – Ramesh.G Jul 29 '17 at 07:48
  • I have only problem of more spacing between cellItem. – NiravS Jul 29 '17 at 07:49
  • increase item width and try changing minimumLineSpacingForSectionAtIndex , minimumInteritemSpacingForSectionAtIndex methods decrease and check can u show screen shot – Ramesh.G Jul 29 '17 at 07:50
1

enter image description here

You can set cells and lines spacing from size inspector.

You can also set from UICollectionViewDelegateFlowLayout.

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat

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

Harshal Valanda
  • 4,911
  • 24
  • 61
0

Add following methods in your code and manipulate until your desire output.

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CGFloat(collectionView.frame.size.width/ 2 - 5), height: CGFloat(135)) // 2 is indicates number of cell in row
    }

    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 10.0
    }

    func  collectionView(_ collectionView: UICollectionView,
                         layout collectionViewLayout: UICollectionViewLayout,
                         minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10.0
    }
KKRocks
  • 7,878
  • 1
  • 14
  • 81
0

My code is all good. But I don't gate the out put what I want so I try new thing that give - minus value

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return -20;
    }

So only using this one method with minus value and I get my desired output. Thanks to all who try to solve this bug for me...

NiravS
  • 896
  • 1
  • 8
  • 22