Skip to content

Instantly share code, notes, and snippets.

@cameronmcefee
Created September 18, 2024 23:50
Show Gist options
  • Save cameronmcefee/a3eaf4848371fdcc5794c4c9700583a3 to your computer and use it in GitHub Desktop.
Save cameronmcefee/a3eaf4848371fdcc5794c4c9700583a3 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
let items = [0, 1, 2, 3, 4]
@State private var position = ScrollPosition(idType: Int.self)
@State private var showPage: Bool = true
let height = 300.0
let width = 300.0
var body: some View {
NavigationStack {
VStack {
Text("This scrolls nicely.")
Text("It's at the navigation root.")
demoView
Spacer()
Button(action: {
showPage = true
}) {
Text("Go to broken version")
}
.buttonStyle(.bordered)
.navigationDestination(isPresented: $showPage) {
VStack {
Text("This studders when scrolling.")
Text("It's in a navigationDestination.")
demoView
Spacer()
}
}
Spacer()
}
}
}
@ViewBuilder var demoView: some View {
let _ = Self._printChanges()
ScrollView(.vertical) {
LazyVStack(spacing: 0) {
ForEach(items, id: \.self) { _ in
Text("Scroll me")
.frame(width: width, height: height)
.border(.pink)
}
}
.scrollTargetLayout()
}
.frame(width: width, height: height)
.border(.blue)
.scrollPosition($position)
}
}
#Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment