Skip to content

Instantly share code, notes, and snippets.

@IntegerOverlord
Created December 8, 2022 10:37
Show Gist options
  • Save IntegerOverlord/4326ebb363c879067265aa1358dbd501 to your computer and use it in GitHub Desktop.
Save IntegerOverlord/4326ebb363c879067265aa1358dbd501 to your computer and use it in GitHub Desktop.
Currently used approach to get List offset
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