Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement non-scrollable UICollectionView inside UIScrollView?

In shorts, my desired screen layout is basically a user profile (iOS 7 + Xcode 5). I used UIScrollView as the top level view. The reason is that I want all its subviews to scroll (user info view - the view with a profile image and some buttons you see on the screen, and the photos collection view - the one with black background) when it is scrolled.

enter image description here

The region with black background will show user photos. I'm wondering if I could use a UICollectionView here, or there's a better way to implement it. The UICollectionView in this case shouldn't be able to scroll itself, it just shows all cells, while the scrolling work is handled by the outermost UIScrollView.

I read following posts:

UICollectionView inside of UIScrollView

UICollectionView in UIScrollView -> Scroll Order

iOS 7 Collection View inside Scroll View

Some said it's not possible (or at least, weird) implemeting UICollectionView inside UIScrollView because UIScrollView is UICollectionView's superclass which leads to unexpected behaviour. Some said it should be implemented in another way (but I didn't see a clear suggestion).

like image 820
Thiem Nguyen Avatar asked Aug 12 '14 04:08

Thiem Nguyen


1 Answers

Yes, you can put a UICollectionView inside a UIScrollView. iOS has fully supported nested scroll views since iOS 3.0, and UICollectionView is a subclass of UIScrollView. For example, check out the App Store app on your iOS device. The screen scrolls vertically - it's either a UIScrollView or a UITableView (which is itself a subclass of UIScrollView). And each row of icons scrolls horizontally - each row is a UICollectionView.

However, it's not clear why you need to put a collection view inside a scroll view. It sounds like you only want the photos view to scroll, so just make the photos view be a collection view. Why do you need to put the collection view inside a scroll view?

UPDATE

Just use a collection view. Set the header of section 0 to the profile info view. You don't need a scroll view.

If you put all the photos in one section, you can set up the header in your storyboard with no code. If you use multiple sections, you'll need to implement collectionView:layout:referenceSizeForHeaderInSection: in your delegate and collectionView:viewForSupplementaryElementOfKind:atIndexPath: in your data source.

like image 102
rob mayoff Avatar answered Oct 21 '22 03:10

rob mayoff