Im trying to make a UICollectionView
that is 1 x N that scrolls horizontally but is locked vertically so it does not move vertically at all.
Any ideas or pointers on how to achieve this?
You need to reduce the height of UICollectionView to its cell / item height and select " Horizontal " from the " Scroll Direction " as seen in the screenshot below. Then it will scroll horizontally depending on the numberOfItems you have returned in its datasource implementation.
A layout object that organizes items into a grid with optional header and footer views for each section.
A layout object determines the placement of cells, supplementary views, and decoration views inside the collection view's bounds and reports that information to the collection view. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.
Use flow layout and set scroll direction to horizontal to fix your problem.
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/2 - 10, height: 190)
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0.0
mainHomeCollectionView.collectionViewLayout = flowLayout
Use the flow layout, and set the scroll direction property to horizontal.
Documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With