I'm displaying a horizontal collection view from right to left and encountering a problem that when displaying the elements, they are displayed from the end (left to right instead):
One solution proposed was adding the code below that worked but required me to reverse the order to add the items. I would like to know if there's a better way to do it and also within the API instead of using an overriding extension
extension UICollectionViewFlowLayout {
open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
return true
}
}
help would be appreciated
code:
import UIKit
class HorizontalScrollTestViewController: UIViewController, UICollectionViewDataSource , CellDelegate{
var items = [String]()
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReusableCell", for: indexPath) as! CategoryCollectionViewCell
cell.category.setTitle(items[indexPath.row] , for: UIControl.State.normal)
cell.delegate = self
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
let dispatchQueue = DispatchQueue(label: "resourcesQueue", qos: .background)
dispatchQueue.async {
self.addItems()
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
collectionView.dataSource = self
collectionView?.register(UINib(nibName: "CategoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ReusableCell")
}
func buttonPressed(_ cell: UICollectionViewCell) {
if let row = self.collectionView.indexPath(for: cell)?.row {
print(items[row])
}
}
func addItems() {
self.items.append("test 10")
self.items.append("test 9")
self.items.append("test 8")
self.items.append("test 7")
self.items.append("test 6")
self.items.append("test 5")
self.items.append("test 4")
self.items.append("test 3")
self.items.append("test 2")
self.items.append("test 1")
}
}
example when adding the items after the app started and using reloadData:
expected behavior
Here you just flip your collectionView
and collectionViewCell
horizontally. this will solve your issue.
With this you can flip your collectionView horizontally. so that your collectionView starts from right. your first element will be at right and last element will be at left.
collectionView.transform = CGAffineTransform(scaleX: -1, y: 1)
OR
collectionView.transform = CGAffineTransform(a: -1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
Similarly you have to flip each collectionViewCell
also using
cell.transform = CGAffineTransform(scaleX: -1, y: 1)
OR
cell.transform = CGAffineTransform(a: -1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
and add element in array in following order
func addItems() {
self.items.append("test 1")
self.items.append("test 2")
self.items.append("test 3")
self.items.append("test 4")
self.items.append("test 5")
self.items.append("test 6")
self.items.append("test 7")
self.items.append("test 8")
self.items.append("test 9")
self.items.append("test 10")
}
Download updated project from Here