I am currently writing a project using SwiftUI and each button in a List presents a sheet. If I dismiss using the dismiss button and select another button in the list, the sheet presents without issues.
However, if I dismiss by swiping down and try to quickly select another button in the list, the sheet does not show unless I tap the button again.
I did some testing regarding this and I found that when I tap the button right after dismissing the sheet, isPresented is still true even though the sheet isn't shown on screen.
My conclusion was that with a dismiss button, isPresented was set to false immediately, but dismissing with a swipe gesture has a noticeable delay when updating isPresented. How would I remove this delay from the swipe gesture?
I tried using sheet(item: ) as well, but some buttons tend to not present a sheet if I quickly dismiss and press a button.
Sample code is attached below. First, present the sheet, dismiss it with a swipe gesture and quickly select another button (or the same button) in the list. The sheet shouldn't present.
// Comment out the ContentView that you don't want to present
struct SheetItem: Identifiable {
let id: UUID = UUID()
let name: String
}
struct ContentView: View {
let items = [
SheetItem(name: "John"),
SheetItem(name: "Dylan"),
SheetItem(name: "Dave")
]
@State private var isPresented = false
@State private var selectedItem: SheetItem?
var body: some View {
NavigationView {
List {
Section("Uses isPresented for the sheet") {
ForEach(items, id: \.id) { item in
Button(item.name) {
// This shows the sheet presentation state on button press
// print(isPresented)
selectedItem = item
isPresented.toggle()
}
}
}
}
.navigationTitle("Results")
}
.sheet(isPresented: $isPresented) {
if let item = selectedItem {
SheetView(item: item)
} else {
Text("Item not selected?")
}
}
}
}
struct ContentView: View {
let items = [
SheetItem(name: "John"),
SheetItem(name: "Dylan"),
SheetItem(name: "Dave")
]
@State private var isPresented = false
@State private var selectedItem: SheetItem?
var body: some View {
NavigationView {
List {
Section("Uses isPresented for the sheet") {
ForEach(items, id: \.id) { item in
Button(item.name) {
// This shows the sheet presentation state on button press
// print(selectedItem)
selectedItem = item
}
}
}
}
.navigationTitle("Results")
}
.sheet(item: $selectedItem) { item in
SheetView(item: item)
}
}
}
struct SheetView: View {
@Environment(\.dismiss) var dismiss
var item: SheetItem
var body: some View {
VStack {
Text("Hello, \(item.name)")
Button("Dismiss") {
dismiss()
}
}
}
}
