Created
December 8, 2022 10:37
-
-
Save IntegerOverlord/4326ebb363c879067265aa1358dbd501 to your computer and use it in GitHub Desktop.
Currently used approach to get List offset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ScrollOffsetPreferenceKey: PreferenceKey { | |
static var defaultValue: CGPoint = .zero | |
static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) {} | |
} | |
struct ScrollOffsetTestView: View { | |
var body: some View { | |
GeometryReader { proxy in | |
List { | |
SwiftUI.Section( | |
header: Color.red | |
.frame(height: 100) | |
.listRowInsets(EdgeInsets()) | |
.anchorPreference(key: ScrollOffsetPreferenceKey.self, value: .bounds) { | |
proxy[$0].origin | |
} | |
.onPreferenceChange(ScrollOffsetPreferenceKey.self, perform: self.handleOffsetChange) | |
) {} | |
ForEach(0..<100) { | |
Text("\($0)") | |
} | |
} | |
} | |
} | |
func handleOffsetChange(to offset: CGPoint) { | |
print("SCROLL OFFSET: \(offset.y)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment