What is the correct way to adjust the width of the UICollectionView to match the number of cells * their width in order to prevent the UICollectionView from creating a second row in my horizontal collection view.
Is there a specific method to implement for the CollectionView which allows me to set this?
Currently looks like:
Currently, I'm implementing the following methods:
func numberOfSections(in collectionView: UICollectionView)
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewDelegateFlowLayout, sizeForItemAt indexPath: IndexPath)
first set your collectionView FlowLayout scrollDirection to horizontal.
let myColl:UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let coll = UICollectionView(frame: .zero, collectionViewLayout: layout)
return coll
}()
then Set your collectionview height constraint equal (or little bit grater) to the cell height
myColl.heightAnchor.constraint(equalToConstant: 50).isActive = true
set your cell size in here
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width-32, height: 50)
}