Jorge
Jorge

Reputation: 137

CollectionView inside ScrollView

So my problem is that every time that I'm trying to scroll, scroll view doesn't work. Only the scroll of the collection view is working.

I have my ViewController and this subviews:

. View
.. Scrollview
... Label
... Image 
... Label
... CollectionView (column of 2)

I tried to disable scroll in collection view but nothing is working as expected.

Upvotes: 11

Views: 25800

Answers (2)

Artem Kirillov
Artem Kirillov

Reputation: 1398

From the information in the question it's hard to give a detailed answer. Just a little tip. You should carefully check 2 important things, when your are working with UIScrollView:

  1. Autolayout constraints of scroll view subviews.
  2. contentSize of your scroll view.

Maybe this will help.

There are also some tips even you choose to disable scrollable for uicollectionView:

  1. You need a fix height for uicollectionView in autolayout, or that will give you a warning: scrollview need y position or height.
  2. You need a constraint outlet to collectionView's height, and adjust its value to contentSize in your code. Such as in viewDidLoad. That means you cannot implement this only using AutoLayout.

Upvotes: 5

Laffen
Laffen

Reputation: 2771

It's not considered good practice to put an UICollectionView inside a UIScrollView because this will result in some unwanted behavior since UICollectionView is a descendant of the UIScrollView class.

Instead of having a UIScrollView with Label, Image, Label, CollectionView, you could remove the UIScrollView and add a UICollectionReusableView as a header to the UICollectionView. Inside this header you can put the Label, Image, Label and that view will always stay on top of the UICollectionView section you created the header for.

You could have a go at this and then come back to us if you run in to some problems :)

UICollectionView

Collection View Basics

Upvotes: 24

Related Questions