Apple recently introduced the UICollectionViewCompositionalLayout
for UICollectionViews
which are amazing. Their only limitation is that when setting the orthogonalScrollingBehavior
to continuous
, the scroll view delegate methods and contentOffset
of the collectionView are missing as the "scrolling behaviour" is accomplished with a totally different scroll view not given to the user.
For a non-essential pure nicety feature I need to access the contentOffset
of the collection view which is always (0, 0)
when using the new UICollectionViewCompositionalLayout
. However if I simply get the abstracted away UIScrollView
then I can check the contentOffset
and make the necessary calculation with it.
My code to achieve this:
let view = collectionView.subviews.first(where: { view in
return String(describing: type(of: view)) == "_UICollectionViewOrthogonalScrollerEmbeddedScrollView"
})
return view as? UIScrollView
I am aware that this may change tomorrow and break this code upon a new version of iOS being released. As I said this is mostly a nicety feature that I would really like to have but it is mostly inessential to the rest of the app. That aside, will this get me rejected for "using" private APIs?
You could use visibleItemsInvalidationHandler to get contentOffset
layoutSection.visibleItemsInvalidationHandler = ({ (visibleItems, point, env) in
print(point)
})