Created
January 30, 2024 20:57
-
-
Save mattyoung/91eff0834176392be76ca6def849c6e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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