@@ -55,6 +55,7 @@ public struct ScrollViewWithStickyHeader<Header: View, Content: View>: View {
5555 /// - headerStretch: Whether to stretch out the header when pulling down, by default `true`.
5656 /// - contentCornerRadius: The corner radius to apply to the scroll content.
5757 /// - showsIndicators: Whether or not to show scroll indicators, by default `true`.
58+ /// - scrollManager: A class that manages programmatic scrolling to header or content.
5859 /// - onScroll: An action that will be called whenever the scroll offset changes, by default `nil`.
5960 /// - content: The scroll view content builder.
6061 public init (
@@ -65,6 +66,7 @@ public struct ScrollViewWithStickyHeader<Header: View, Content: View>: View {
6566 headerStretch: Bool = true ,
6667 contentCornerRadius: CGFloat = 0 ,
6768 showsIndicators: Bool = true ,
69+ scrollManager: ScrollManager ? = nil ,
6870 onScroll: ScrollAction ? = nil ,
6971 @ViewBuilder content: @escaping ( ) -> Content
7072 ) {
@@ -75,6 +77,7 @@ public struct ScrollViewWithStickyHeader<Header: View, Content: View>: View {
7577 self . headerMinHeight = headerMinHeight
7678 self . headerStretch = headerStretch
7779 self . contentCornerRadius = contentCornerRadius
80+ self . scrollManager = scrollManager
7881 self . onScroll = onScroll
7982 self . content = content
8083 }
@@ -86,11 +89,12 @@ public struct ScrollViewWithStickyHeader<Header: View, Content: View>: View {
8689 private let headerMinHeight : Double ?
8790 private let headerStretch : Bool
8891 private let contentCornerRadius : CGFloat
92+ private let scrollManager : ScrollManager ?
8993 private let onScroll : ScrollAction ?
9094 private let content : ( ) -> Content
91-
95+
9296 public typealias ScrollAction = ( _ offset: CGPoint , _ visibleHeaderRatio: CGFloat ) -> Void
93-
97+
9498 @State
9599 private var scrollOffset : CGPoint = . zero
96100
@@ -148,16 +152,23 @@ private extension ScrollViewWithStickyHeader {
148152 func scrollView(
149153 in geo: GeometryProxy
150154 ) -> some View {
151- ScrollViewWithOffsetTracking (
152- axes,
153- showsIndicators: showsIndicators,
154- onScroll: handleScrollOffset
155- ) {
156- VStack ( spacing: 0 ) {
157- scrollHeader
158- . opacity ( 0 )
159- content ( )
160- . frame ( maxHeight: . infinity)
155+ ScrollViewReader { scrollProxy in
156+ ScrollViewWithOffsetTracking (
157+ axes,
158+ showsIndicators: showsIndicators,
159+ onScroll: handleScrollOffset
160+ ) {
161+ VStack ( spacing: 0 ) {
162+ scrollHeader
163+ . opacity ( 0 )
164+ . id ( ScrollManager . ScrollTargets. header)
165+ content ( )
166+ . frame ( maxHeight: . infinity)
167+ . id ( ScrollManager . ScrollTargets. content)
168+ }
169+ }
170+ . onAppear {
171+ scrollManager? . setProxy ( scrollProxy)
161172 }
162173 }
163174 }
0 commit comments