I have used, with success (and headaches :), using three UICollectionView
s within a single UIScrollView
. Make the UIScrollView
span the entire screen height and add paging with your tab menu. Add your tab menu/header content above the scroll view (so it is higher in the view hierarchy and covers the top half of your UIScrollView
. Use each collection view's scrollViewDidScroll
method to get the scrollView.contentOffset.y
value and adjust a y anchor of your menu accordingly. If the contentOffset.y
value gets taller than your menu, stop adjusting it to "lock" the tab bar part on top. The trick (and pain) was to make the first cell in each collection view an empty filler. This filler cell should be the same size as your tab menu/header content so the collection view appears as if it's in the right spot and not hidden by your tab menu/header content.
An example implementation of scrollViewDidScroll
. The tabMenuHeaderContentTopAnchor
is an NSContstraint
. 150 happened to be the height of my header content.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let constant = -scrollView.contentOffset.y
if constant < -150 { // This will lock your tab menu in place
tabMenuHeaderContentTopAnchor?.constant = -150
} else { // Otherwise, adjust the anchor to move with the collection view
tabMenuHeaderContentTopAnchor?.constant = constant
}
}
Benefits of this I found:
- Easy to add pages to your
UIScrollView
as needed
- Each collection view is managed separately by its own data source
Negatives I found:
- It gets a little complicated handling the different content offsets of multiple collection views