iosswiftuikitcollectionviewuicollectionviewcompositionallayout

Cannot disable Section bouncing in CollectionView - Compositional Layout


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!


Solution

  • 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