Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollectionView inside ScrollView

Tags:

ios

swift

swift3

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.

like image 662
Jorge Avatar asked Nov 08 '17 07:11

Jorge


People also ask

Is CollectionView scrollable?

CollectionView defines two ScrollTo methods, that scroll items into view. One of the overloads scrolls the item at the specified index into view, while the other scrolls the specified item into view.

What is Uicollectionreusableview?

A view that defines the behavior for all cells and supplementary views presented by a collection view.

What is swift Uicollectionview?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is scroll view in Swift?

The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.


2 Answers

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.
like image 83
Artem Kirillov Avatar answered Oct 14 '22 00:10

Artem Kirillov


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

like image 20
Laffen Avatar answered Oct 13 '22 22:10

Laffen