Skip to content

Instantly share code, notes, and snippets.

@jpsim
Created October 12, 2023 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpsim/2bf987f6f64bb0987acbf1970af93328 to your computer and use it in GitHub Desktop.
Save jpsim/2bf987f6f64bb0987acbf1970af93328 to your computer and use it in GitHub Desktop.
[FB13265427] ScrollView + LazyVStack is jittery starting with iOS 17

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:

  1. Tap 1
  2. Tap 3
  3. Scroll down
  4. 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment