4

I'm using UICollectionViewCompositionalLayout and the *UICollectionLayoutSectionOrthogonalScrollingBehavior to build a carousel.

The scrollToItem() method will correctly scroll to the item at the specified indexPath but will create a top-inset (animates it smoothly). It doesn't inset the rest of the content though, which will make the cell overlay subsequent content. (Here a section footer with a page control)

This top-inset will be the size of a section header + the contentInset of the layout section. scroll issue

Any subsequent scrolling initiated by the user will cause the layout to update and make the cell jump to the correct position.

The problem occurs only when using one of the .paging behaviours for orthogonal scrolling.

I've tried to mess with the layout item in visibleItemsInvalidationHandler but I have not found a way to distinguish whether the handler is called because the user scrolled or because the scrollToItem() method was called programmatically.

Check out the sample project on github

I'm using Xcode 13.0 and iOS 15.0

David Schmoecker
  • 567
  • 3
  • 8
  • 17
  • 1
    Closest solution I came is to "pageControl" the item to its correct position, but without animation: first, call `scrollToItem()` with `animated: false`, and then invalidate the layout with `collectionView.collectionViewLayout.invalidateLayout()`. This allows the item to be positioned correctly, but there is no animation. – Sardorbek Ruzmatov Nov 14 '21 at 18:30
  • 1
    Thanks for having a go at it! My current workaround is to use .continuousGroupLeadingBoundary as scrolling behaviour. The animation will work, but the swiping will "feel wrong". – David Schmoecker Nov 14 '21 at 21:05

0 Answers0