3

I have a collectionView with compositional layout which scrolls vertically, with each section inside it scrolling horizontally.

I would need to disable bouncing on horizontal scroll, which does not seem to be possible (or maybe I'm doing something wrong?). Disabling vertical bouncing works perfectly.

What I have tried so far:

    collectionView.contentInsetAdjustmentBehavior = .never
    collectionView.bounces = false
    collectionView.alwaysBounceHorizontal = false
    collectionView.alwaysBounceVertical = false
    collectionView.isDirectionalLockEnabled = true

This does not seem to work for horizontal bouncing - it is still enabled. My compositional layout is created in this way:

func createCompositionalLayout() {
        let layout = UICollectionViewCompositionalLayout { sectionIndex, _ in
            let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(UIScreen.main.bounds.width), heightDimension: .absolute(UIScreen.main.bounds.height))
            let item = NSCollectionLayoutItem(layoutSize: itemSize)

            let group = NSCollectionLayoutGroup.horizontal(layoutSize: itemSize, subitems: [item])
            group.interItemSpacing = .fixed(0)

            let section = NSCollectionLayoutSection(group: group)
            section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
            section.orthogonalScrollingBehavior = .paging
            return section
        }
        collectionView.collectionViewLayout = layout
    }

If anyone might know how to solve the issue, any help would be much appreciated!

  • I might be wrong, but I have double checked with the collection views [here](https://developer.apple.com/documentation/uikit/views_and_controls/collection_views/implementing_modern_collection_views) and it did work just fine. I believe in your case it is not working because the content is bigger than bounds. And it also says in the description `// default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally.` Maybe you could easily check that by having only 1 item in your section (your item size is screen width)and it shouldn't bounce. Let me know – Joke coder Jan 04 '23 at 20:47
  • 1
    after some toying around, it does seem a little odd. If your items in the section are bigger than bounds then it doesn't work for horizontal, but works for vertical... – Joke coder Jan 04 '23 at 21:01
  • hey @Jokecoder, thanks for your reply! I believe the "alwaysBounceHorizontal" property only defines the bounce behavior in case if the item size is less than the screen bounds. By default Apple have implemented the non-bouncing behavior for this case, but with this property they gave the opportunity to change it if we want. Strange that there is no visible API to change the bouncing behavior for the whole section if it's size is bigger than bounds! Or at least I wasn't able to find one since yesterday – Anastasia Veremiichyk Jan 05 '23 at 10:52

1 Answers1

2

After spending almost 2 days with the Apple's example class OrthogonalScrollingViewController, it does look weird that the bouncing (on/off) works only for the vertical scrolling.

collectionView.bounces = false
collectionView.alwaysBounceHorizontal = false
collectionView.alwaysBounceVertical = false

This won't work because:

Short answer

Because the collection view's section (compositional layout's section) is not affected by the scroll view at all

Long answer

If you conform to UIScrollViewDelegate (UICollectionViewDelegate has it already, hence no need for that, if you have conformed to the latter), the method doesn't even get called

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        //something should happen
    }

and that's because our collection view is vertical -> scroll view direction is vertical and our sections are not affected by it (ever?! really?!)

A discussion that helped me:

UICollectionView CompositionalLayout not calling UIScrollDelegate

Joke coder
  • 247
  • 1
  • 10