UICollectionView: How to define a UICollectionViewLayout that supports horizontally and vertically scrolling?
Asked Answered
C

4

19

At the moment I'm trying to create an UICollectionView, that should display a simple excel-like-spreadsheet, with rows and columns. This should be an easy task with UICollectionViews, I believed. And I really would like to do the implementation in UICollectionView, not in any grid framework.

But at the moment I'm hanging a little bit. What I've figured out is, that I unfortunately can't use a UICollectionViewDelegateFlowLayout, because this only supports scrolling in either horizontally or vertically direction. But I need scrolling in both directions.

Therefore I have to use a UICollectionViewLayout, but for this I didn't find good examples, how to use it. Has anyone of you an example, how to subclass an UICollectionViewLayout to support rows and columns?

Thanks in advance.

Cayla answered 7/11, 2012 at 8:54 Comment(0)
G
5

UICollectionViewFlowLayout definitely can't - "The grid layout scrolls along one axis only, either horizontally or vertically."

It is a subclass of UICollectionViewLayout. Seems like you could create your own subclass of UICollectionViewLayout and scroll in both directions.

What do you mean by page, you want to do paging like a scrollview? I'm wondering about using UICollectionViewFlowLayout in a UICollectionView for vertical scrolling, inside a UIScrollView for horizontal scrolling.

Gery answered 7/11, 2012 at 9:36 Comment(1)
Yes, I know already, that I have to use UICollectionViewLayout instead of UICollectionViewFlowLayout... this I've posted already in the initial question. So your comment did absolutely not answer the question, sorry.Cayla
S
5

I've added an example of using a custom layout to implement a simple horizontal & vertical scrolling grid here https://github.com/neildavis/MyCollectionView

My example uses PSTCollectionView since I needed iOS 5 support, but it's API compatible with built in UICollectionView. (Actually it uses the compatibility classes (PSUICollectionView etc) provided to ensure it uses a UICollectionView on iOS 6 and falls back to using PSTCollectionView etc on iOS 5.) Just remove the 'PS' prefix on these to revert to iOS 6 only UIKit implementation.

Sentient answered 25/4, 2013 at 13:25 Comment(0)
G
4

I needed something similar. I created a Custom CollectionViewLayout.

https://github.com/akashraje/BidirectionalCollectionViewLayout

Ginkgo answered 2/8, 2015 at 15:21 Comment(2)
This worked brilliantly for me, though had to be careful with the spelling of the component as it isn't automatically picked up by Xcode, and a small typo had me going around in circles for ages.Restriction
Great! With this source you can also implement a selection for a row of items.Geralyngeraniaceous
A
3

Answer that doesn't require dissecting a 3 sample projects

In your UICollectionViewLayout subclass:

  1. Implement -(CGSize)[collectionViewContentSize] and return a size larger in height and width than the UICollectionView's size. (Accessible via: self.collectionView.frame.size)
  2. Calculate layout for your UICollectionViewCells within the content size rather than the frame size.

Note: Make sure scrolling is enabled in the UICollectionView. (It's on by default.)

Accouplement answered 8/1, 2015 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.