I am noticing a rather strange, possibly buggy behavior in SwiftUI using iOS 13.3.1 and earlier. To demonstrate, this very simple scenario:
Full sample code:
import SwiftUI
struct ContentView: View {
@State var showingSheet: Bool = false
var body: some View {
NavigationView {
Text("ParentView")
.navigationBarItems(trailing: Button(action: { self.showingSheet = true }) { Text("Show") })
}.sheet(isPresented: $showingSheet) { SheetView() }
}
}
struct SheetView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
Text("Sheet content")
Button(action: { self.presentationMode.wrappedValue.dismiss() }) { Text("Hide") }
}
}
}
To reproduce:
Expected result: The sheet shows again.
Actual result: The tap target of the "Show" button is shifted some pixels to the bottom, resulting in the action not triggering. This can also be observed in the View Hierarchy Debugger:
Other observations:
So now I'm here wondering: Am I making a mistake, or is this an actual SwiftUI bug? If so, is there an easy workaround? I searched SO for similar issues, but didn't come across any that were identical.
Edit: I filed this with Apple as Radar Feedback FB7560960 in the meantime.
Edit 2: Still happening in 13.4.1.
I don't know why, but adding
@Environment(\.presentationMode) var presentationMode
to contentView
seems to fix it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With