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.
r]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) {
}
}