2

In my collection view I am getting extra space at the end of the last cell. The space also increases if the number of cells increases. The background color of collection view is yellow. How can I stop this extra space? In Second Image the Height And Width of the Cell Is half of that of the Collection View.

ImageofSimulato[Image Secondr]2

This is my code for ViewController.swift File

import UIKit

class ViewController: UIViewController ,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{

    @IBOutlet weak var collectionview: UICollectionView!
    @IBOutlet weak var pagecontrol: UIPageControl!
    @IBOutlet weak var VarBtnGet: UIButton!
    @IBOutlet var MainView: UIView!
    @IBOutlet weak var ScrollView: UIScrollView!
    @IBOutlet weak var SubScrollView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        btnsetuplayout()
        pagecontrolsetup()
        collectionview.dataSource = self
        collectionview.delegate  = self
        collectionviewsetup()




    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5

    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath)
        cell.backgroundColor = indexPath.item % 2 == 0 ? .green : .red

        return cell

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 0
    }
    func btnsetuplayout()
    {
        VarBtnGet.translatesAutoresizingMaskIntoConstraints = false
        VarBtnGet.bottomAnchor.constraint(equalTo:SubScrollView.bottomAnchor,constant: -5).isActive = true
               VarBtnGet.leftAnchor.constraint(equalTo: SubScrollView.leftAnchor ,constant: 0
               ).isActive = true
               VarBtnGet.rightAnchor.constraint(equalTo: SubScrollView.rightAnchor,constant: 0).isActive = true

    }
    func pagecontrolsetup()
    {
        pagecontrol.translatesAutoresizingMaskIntoConstraints = false
        pagecontrol.bottomAnchor.constraint(equalTo:  VarBtnGet.topAnchor,constant: -100  ).isActive = true
        pagecontrol.centerXAnchor.constraint(equalTo:  SubScrollView.centerXAnchor).isActive = true
        pagecontrol.backgroundColor = .brown


    }
    func collectionviewsetup()
    {

        collectionview.translatesAutoresizingMaskIntoConstraints = false
        collectionview.isPagingEnabled = true
     //   collectionview.frame =  CGRect.zero
        if let layout = collectionview.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
        }

        collectionview.backgroundColor = .yellow
        collectionview.topAnchor.constraint(equalTo : SubScrollView.topAnchor ,constant: 5 ).isActive = true

          collectionview.heightAnchor.constraint(equalTo: SubScrollView.heightAnchor,multiplier: 0.5).isActive = true
        collectionview.widthAnchor.constraint(equalTo:SubScrollView.widthAnchor).isActive = true

         //collectionview.leftAnchor.constraint(equalTo: SubScrollView.leftAnchor,constant: 5) .isActive=true
         //collectionview.rightAnchor.constraint(equalTo: SubScrollView.rightAnchor,constant: -5) .isActive=true
    }


    @IBAction func ButtonGet(_ sender: Any) {
    }

}

2 Answers2

1

Did you check the minimum spacing between items and set it to 0 with minimumInteritemSpacing? It defaults to 10.0 so might explain what you're seeing. I see you set the line spacing to 0 so that's good.

Mark Thormann
  • 2,351
  • 1
  • 16
  • 20
0

i know that is an old post but i had the same issue and i found a solution. I hope that this answer could be helpful for someone. In my case the i solved my problem changing the collection view property Estimated size from Automatic to None. In my case this solved the problem.

enter image description here

Set Estimate Size to none using storyboard in order to make it work properly. If you will not set that than below code will not work as expected.

Alessandro
  • 2,617
  • 1
  • 8
  • 13