I need a UICollectionView
to display a grid that is potentially larger than the visible frame in both width and height, while maintaining row and column integrity. The default UICollectionViewFlowLayout
allows sections to scroll off-screen, but it wraps items within a section to keep them all on-screen, which screws up my grid.
Recognizing that UICollectionView
is a subclass of UIScrollView
, I tried just manually setting the collection view's content size property in viewDidLoad:
self.collectionView.contentSize = CGSizeMake((columns * (cellWidth + itemSpacingX), (rows * (cellHeight + itemSpacingY));
But this had no effect. Questions:
- Is there an easy way to accomplish this without building a custom layout?
- Would using a custom layout and overriding the
collectionViewContentSize
method succeed in getting the collection view to stop wrapping items and scroll in both directions? - If I do have to build a custom layout--which I'll have to invest some time to learn--do I subclass
UICollectionViewLayout
, or would subclassingUICollectionViewFlowLayout
save time?
UPDATE: I tried embedding the UICollectionView as a subview of a UIScrollView. The collection view itself is behaving correctly--the rows aren't wrapping at the edge of the scroll view, telling me that it is filling the UIScrollView content size that I set. But the scroll view doesn't pan in the horizontal direction, i.e. it only scrolls vertically, which the collection view does by itself anyway. So stuck again. Could there be an issue with the responder chain?
self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
got the job done for me. I wonder if this would work in your situation. – Retrorocket