0

I'm having trouble setting my UICollectionViewCells to be all the same width and height so that there are no lines/spacing between each cell. Testing on an smaller iPhone like 5S and SE shows no lines between the cells, but larger iPhones such as 6,7 Pluses do - image description below. My view controller adopts UICollectionViewDelegateFlowLayout, and it has a collectionView.

In viewDidLoad, I set the collectionView's delegate to be the view controller.

override func viewDidLoad() {
    super.viewDidLoad()
    self.viewController.delegate = self
}

I use the following methods to set the cell size and spacing.

let screenWidth:CGFLoat = UIScreen.main.bounds.width
let cellsPerRow:CGFLoat = 4

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let side:Double = Double(self.screenWidth/self.cellsPerRow)
    return CGSize(width: side, height: side)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0.0
}

iPhone 5S, No Lines iPhone 5s iPhone 7 Plus, Lines iPhone 7 Plus Any recommendations or advice to get rid of these line/spacing is greatly appreciated!

Miket25
  • 1,895
  • 3
  • 15
  • 29
  • Try setting `self.automaticallyAdjustsScrollViewInsets = false` in `viewDidLoad ` – lionserdar Jan 15 '18 at 01:25
  • @lionserdar That did not resolve it – Miket25 Jan 15 '18 at 01:33
  • I think this is a floating number issue, probably that's why it's happening.Instead of 4 try 3 and see what happens? maybe you can change your collectionview background color to black ? I know it's kind a hacky but it may do the job for ya. – lionserdar Jan 15 '18 at 02:04
  • @lionserdar Yes it is because of the division by four. The cells per row needs be four. Unfortunately, setting the background black won't work since these cells will take on any color that's dependent on the user. – Miket25 Jan 15 '18 at 02:21
  • check this post https://stackoverflow.com/questions/40131349/enforce-collectionview-to-have-only-2-rows/40133928#40133928 and https://stackoverflow.com/questions/40413273/how-to-set-the-collection-view-cell-size-exactly-equal-to-the-collection-view-in/40413849#40413849 – Joe Jan 15 '18 at 04:42

0 Answers0