1

I am using UICollectionViewDelegateFlowLayout delegate to make it square but strange spacing is showing at the end.

enter image description here enter image description here

My Code

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

    if collectionView == self.bannerClcView {

        let cellSize = CGSize.init(width: view.frame.size.width, height: view.frame.size.height)
        return cellSize

    }else if collectionView == self.category {

        let cellSize = CGSize.init(width: (self.category.frame.size.width/3), height: category.frame.size.width/3)
        return cellSize

    }else {

        let cellSize = CGSize.init(width: (recomended_Ads_Clc.frame.size.width/2), height: 220)
        return cellSize

    }
}


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

How can I remove the spacing? Please help.

hgwhittle
  • 9,316
  • 6
  • 48
  • 60
Dhiru
  • 3,040
  • 3
  • 25
  • 69
  • Do you want to stretch your cells? or move them to the middle? – Razi Tiwana Apr 12 '19 at 12:40
  • 1
    You might check if the right-side AutoLayout constraint for your `UICollectionView` is attached to the 'safe-area'. Seems like you'd rather it be attached to the superview. – hgwhittle Apr 12 '19 at 12:43
  • cell should cover full size of CollectionView , in first , i want 3 cell only covering whole width , yes i want to stretch the cell , it should be equal to the exactly collectionView.width/3 . @RaziTiwana – Dhiru Apr 12 '19 at 12:46
  • @hgwhittle , oh i think this issue , working fine on iphone 8 , can you please tell me how to remove constraint from `safe-area` ? – Dhiru Apr 12 '19 at 12:50
  • try this. 10 is the gap between two cell in my case.... func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: (collectionView.frame.width/2)-10 , height: collectionView.frame.height) } – Ben Rockey Apr 12 '19 at 12:57
  • also set collectionView View Content Mode to Scale To Fill. – Ben Rockey Apr 12 '19 at 13:05
  • @Dhiru did you lay out your collection view using storyboards? – hgwhittle Apr 12 '19 at 13:17

2 Answers2

0

If you open your Storyboard where you've laid out your UICollectionView and select its trailing constraint, I image you'll see something like this in the Attributes inspector:

enter image description here

You'll need to uncheck "Relative to margin" to remove the extra space you're seeing.

hgwhittle
  • 9,316
  • 6
  • 48
  • 60
0

Your mistake is with the source view that you compute the width property for the cell inside the function

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

instead of

let cellSize = CGSize.init(width: (recomended_Ads_Clc.frame.size.width/2), height: 220) return cellSize

write return CGSize(width: (collectionView.frame.width / 2) - 10, height: 220)

And compute the height as you want. 10 will be the space between the cells

Vasilis D.
  • 1,416
  • 13
  • 21