I'm creating a horizontal scroll view through the collection view.
I used flowlayout for layout, and I set the scroll direction to horizontal.
I found a bug in this situation.
If the width of the item is different, line spacing is not applied, but item spacing is applied.
Since it is a horizontal scroll, line spacing should be applied in the direction of the column, but item spacing is applied.
Does anyone know anything about this?
//
// ViewController.swift
// minimumLineSpacingTest
//
// Created by Hoonki chae on 2023/07/12.
//
import UIKit
class TestCell: UICollectionViewCell {
}
class ViewController: UIViewController {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.collectionView.frame = .init(origin: .init(x: 0, y: 100), size: .init(width: self.view.frame.width, height: 50))
self.view.addSubview(self.collectionView)
self.collectionView.backgroundColor = .systemGray.withAlphaComponent(0.2)
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(TestCell.self, forCellWithReuseIdentifier: "Cell")
(self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.scrollDirection = .horizontal
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .green.withAlphaComponent(0.3)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return .init(width: indexPath.item > 2 ? 150 : 140, height: 50)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 20
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 4
}
}