/ios-nested-scrollviews

A very simple UIKit implementation of nested UIScrollViews

Primary LanguageSwift

iOS Simple Nested Scrollviews

Nested scrollviews are problematic in UIKit because of the following reasons.

  1. Nested scrollview's scroll gestures are sometimes 'stolen' by outer scrollview.
  2. Smooth scroll transition between scrollviews is not built-in.
  3. Fixing this requires libraries or hijacking scroll gestures in the UIScrollViewDelegate.

The proposed solution for achieving this in UIKit is as follows.

  1. Set the outer scrollview's class to OuterScroll.
  2. Set the inner scrollview's class to InnerScroll.
  3. Add the code, OuterScroll.Reference.iScroll = <InnerScroll instance>

Both OuterScroll and InnerScroll inherit from UIScrollView.

Usage

1. Setup two UIScrollViews where the nested one is inside the outer one.

2. Create the relationship between Outer and Inner UIScrollViews

class ViewController: UIViewController {

    @IBOutlet private weak var outerScroll: OuterScroll!
    @IBOutlet private weak var innerScroll: InnerScroll!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Set references
        OuterScroll.Reference.iScroll = innerScroll
    }
}