Nested scrollviews are problematic in UIKit
because of the following reasons.
- Nested scrollview's scroll gestures are sometimes 'stolen' by outer scrollview.
- Smooth scroll transition between scrollviews is not built-in.
- Fixing this requires libraries or hijacking scroll gestures in the
UIScrollViewDelegate
.
The proposed solution for achieving this in UIKit
is as follows.
- Set the outer scrollview's class to
OuterScroll
. - Set the inner scrollview's class to
InnerScroll
. - Add the code,
OuterScroll.Reference.iScroll = <InnerScroll instance>
Both OuterScroll
and InnerScroll
inherit from UIScrollView
.
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
}
}