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

荒凉一梦 提交于 2019-11-28 03:36:24

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.

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.

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.)

I needed something similar. I created a Custom CollectionViewLayout.

https://github.com/akashraje/BidirectionalCollectionViewLayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!