Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created January 30, 2024 20:57
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 mattyoung/91eff0834176392be76ca6def849c6e6 to your computer and use it in GitHub Desktop.
Save mattyoung/91eff0834176392be76ca6def849c6e6 to your computer and use it in GitHub Desktop.
import SwiftUI
struct BottomSheet: View {
@State private var showSheet = false
@State private var scrollToId = 0
@State private var listView = true
var myList: some View {
ScrollViewReader { proxy in
VStack {
Button("Scroll to \(scrollToId)") {
proxy.scrollTo(scrollToId)
}
if listView {
List {
ForEach(0..<500) { n in
Button("\(n)") {
scrollToId = n
showSheet = false
}
.id(n)
}
}
} else {
let columns = [GridItem(.adaptive(minimum: 50))]
ScrollView(.vertical) {
LazyVGrid(columns: columns, spacing: 5) {
ForEach(0..<250) { n in
Button("\(n)") {
scrollToId = n
showSheet = false
}
.id(n)
}
}
}
}
}
.onAppear {
// want to scroll to the last press item on the list
// but doesn't seem to work with ScrollView/LazyVGrid
// but works with simple list
proxy.scrollTo(scrollToId)
}
}
}
var body: some View {
VStack {
Button("Show List") {
showSheet.toggle()
listView = true
}
Button("Show Grid") {
showSheet.toggle()
listView = false
}
Text("scrollToId: \(scrollToId)")
}
.sheet(isPresented: $showSheet) {
myList
.presentationDetents([.large])
}
}
}
#Preview {
BottomSheet()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment