I'm trying to use the new (announced at WWDC 2023 and available in iOS 17 and macOS 14) API to observe scroll position of SwiftUI ScrollView
.
I have the following view, but I can't make it work correctly. That is, using the scrollPosition(id:anchor)
view modifier makes the scroll view go nuts. At some point, and in some cases even at the very begging, the scroll view just starts jumping and is stuttering when trying to scroll. See the screen recording here: https://twitter.com/krajaac/status/1714191037927764149
The code:
ScrollView(.vertical) {
LazyVGrid(
columns: [.init(.adaptive(minimum: self.itemSize.width), spacing: Constants.columnSpacing, alignment: .center)],
alignment: .center,
spacing: Constants.sectionSpacing
) {
ForEach(self.sectionData) { section in
Section(section.title) {
ForEach(section.items) { item in
Text(item.text)
.frame(width: self.itemSize.width, height: self.itemSize.height)
}
}
}
}
.scrollTargetLayout()
}
.scrollPosition(id: self.$currentItemID) // TODO: Comment out this line to make the scrolling work
The whole code can be found in my GitHub repo: https://github.com/tomaskraina/feedbackassistant.apple.com/blob/master/ScrollPositionVsLazyVGrid/ScrollPositionVsLazyVGrid/ContentView.swift
Am I doing something wrong or is this a bug in SwiftUI?
I still haven't figured out the root cause of this, but "hiding" the content of the
ScrollView
--LazyVGrid
in a customView
like this fixes the issues with scrolling for some reason:See the screen recording here: https://mastodon.social/@tomkraina/111251021544613267