David Lara
David Lara

Reputation: 51

How to enable scroll in UICollectionView?

I’m still learning to develop in iOS and Swift so I apologize beforehand if my question is too simple.

I have created an UICollectionViewController to show a matrix of elements. My issue is that the width of the screen is not wide enough to fit all the columns in a single row, so the excess of them are shown in another row below. Instead of this, I’d like to enable the horizontal scroll so the user can scroll to see all the columns in one single row.

I know UICollectionViewController already contains UIScrollView so I guess this should be as simple as to change a setting in the .storyboard but I couldn’t find it after many trial-errors :(

I guess it is related to the "Flow" layouts setting and that I need to handle a custom one, but don't know how exactly.

Would anyone be so kind to please help me? I haven’t attached any code because I’m using a pretty standard/out-of-the-box implementation of UICollectionViewController but if you want me to add anything, just let me know, please.

Many thanks in advance for any help!

Upvotes: 0

Views: 4438

Answers (2)

David Lara
David Lara

Reputation: 51

I found the answer to my issue perfectly covered on this post:

https://www.credera.com/blog/mobile-applications-and-web/building-a-multi-directional-uicollectionview-in-swift/

Upvotes: 1

Tommy
Tommy

Reputation: 100622

It's definitely to do with your UICollectionViewLayout — that tells the collection view where everything should go. The collection view simply picks its scroll behaviour appropriately.

A UICollectionViewFlowLayout is a specific type of layout that fills one row column to the size of the enclosing view, then moves on to the next. So it does one-dimensional scrolling. Does that fit your use case?

If so then you should just be able to set the scroll direction on the flow layout.

If not then you'll need to write a custom subclass of UICollectionViewLayout that implements the layout behaviour you want.

Upvotes: 0

Related Questions