When a LazyVStack is nested within a ScrollView on iOS 17, the scroll view's content offset can be incorrectly set for a brief moment, causing the scroll view to "jitter".
Here's some sample code:
import SwiftUI
struct ContentView: View {
@State var isExpanded = Set<Int>()
var body: some View {
ScrollView {
LazyVStack {
ForEach(0..<10, id: \.self) { index in
Rectangle()
.fill(.orange)
.overlay {
Text("\(index)")
.font(.title)
}
.frame(height: isExpanded.contains(index) ? 200 : 100)
.onTapGesture {
withAnimation {
if isExpanded.contains(index) {
isExpanded.remove(index)
} else {
isExpanded.insert(index)
}
}
}
.padding(.bottom, 10)
}
}
}
}
}
To reproduce, run the provided sample code on an iOS 17 simulator or device and follow these steps:
- Tap 1
- Tap 3
- Scroll down
- Tap 7
When tapping 7, notice how the scrollview shifts up an back down for a split second.
This bug does not happen on iOS 16.4, but does happen on iOS 17.0. Changing the LazyVStack for a VStack also avoids the issue.
This was tested with Xcode Version 15.0 (15A240d) running on an iPhone SE (3rd generation) running iOS 17.0 (21A328).