I was working on pagination and needed a few values from the ScrollView. I came across this question, which contains an answer, but it also includes a few other bonus tips. Keep in mind that this is available on iOS 18.0 and above.
Hopefully someone could use this as a foundation and make it better:
ScrollView {
// Your content
}
.onScrollGeometryChange(for: [Double].self, of: { geometry in
[
geometry.contentSize.height,
geometry.contentOffset.y,
geometry.bounds.height
]
}, action: { _, newValue in
let contentHeight = newValue[0]
let offsetY = newValue[1]
let scrollViewHeight = newValue[2]
// print(contentHeight, offsetY, scrollViewHeight)
// print(vm.currentOffset)
if offsetY > contentHeight - scrollViewHeight {
// method for pagination
}
})
ScrollViewas well.