Xcode: Version 11.3 (11C29)
Target: Simulator running iOS 13.3
I'm getting inconsistent behaviour that's hard to diagnose when working with sheets and SwiftUI. When I:
* Use a button in the NavigationView
itself, I can consistently call up the sheet and dismiss it
* Use a button in the bar of the NavigationView
(i.e. navigationBarItems
), the behaviour is inconsistent: sometimes it toggles without issue, sometimes it will "lock up" and not respond for ~10 seconds before functioning again. Swiping around and performing other actions in the interface seem to help in "resetting" the functionality.
Minimum reproducible example:
import SwiftUI
struct ContentView: View {
@State var isModalShowing = false
var body: some View {
NavigationView {
VStack {
List {
ForEach(1..<5) { number in
Text(String(number))
}
}
Button("First Modal Button") {
self.isModalShowing = true
}
}
.navigationBarItems(leading:
Button("Second Modal Button") {
self.isModalShowing = true
})
}
.sheet(isPresented: $isModalShowing) {
TestView(isPresented: self.$isModalShowing)
}
}
}
struct TestView: View {
@Binding var isPresented: Bool
var body: some View {
VStack {
Text("Hello SwiftUI!")
Button("Dismiss") {
self.isPresented = false
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
To note:
* Pressing First modal button
works consistently
* Pressing Second modal button
works inconsistently after dismissing the modal for the first time (try using this button ~5-10 times in a row, it should lock up, but it's hard to predict when it will)
There's already a few StackOverflow questions/answers about this, but none have worked for me so far and they seem to talk about the Xcode Betas, whereas I am on stable (hopefully 11.3 is stable). Different ways of passing the state (@Binding
vs @Environment
) didn't help. Moving around where the .sheet
is called also didn't change anything. I think it's also related to the List
and/or ForEach
but I'm new to Swift and not sure how to debug further.