SwiftUI sheet gets dismissed the first time it is presented

Sometimes (well most of the time) presented sheet gets dismissed first time it is opened. This is happening only on a device and only the first time the app is launched. This is the code:

Code Block
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailsView()) {
Text("Open Details View")
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct DetailsView: View {
@State var sheetIsPresented: Bool = false
var body: some View {
VStack {
Button("Open") {
sheetIsPresented.toggle()
}
}.sheet(isPresented: $sheetIsPresented, content: {
SheetView()
})
}
}
struct SheetView: View {
var body: some View {
Color.red
}
}

I was able to fix this problem by removing line .navigationViewStyle(StackNavigationViewStyle()), but I need StackNavigationViewStyle in my project. Any help will be appreciated.

I tested your exact code in simulator with Xcode 12.1.
I Tapped Open several times.
I tapped Open, dismissed the view (swipe down), then tapped back, then open Details and Open again.
Could not replicate the problem in any case.

Tested as well with Xcode 12.2ß,
No problem either.

Tested with Xcode 12GM
Works OK as well.

Which version of Xcode do you use ?
@Sash
I could have confirmed that the odd behavior you have described happens on my iPhone 7+/iOS 14.1.
(One note, you should better include the info of the devices you have tested when reporting something happening only on a device.)

It looks like one of the weird behaviors reported in this thread:
iOS 14 (SwiftUI) Sheet Modals not dismissing and acting randomly weird

As is reported in the SOLVED answer, moving sheet out of NavigationLink can be a workaround:
Code Block
struct ContentView: View {
@State var sheetIsPresented: Bool = false
var body: some View {
NavigationView {
NavigationLink(destination: DetailsView(sheetIsPresented: $sheetIsPresented)) {
Text("Open Details View")
}
}
.navigationViewStyle(StackNavigationViewStyle())
.sheet(isPresented: $sheetIsPresented, content: {
SheetView()
})
}
}
struct DetailsView: View {
@Binding var sheetIsPresented: Bool
var body: some View {
VStack {
Button("Open") {
sheetIsPresented.toggle()
}
}
}
}


If this is hard to apply to your actual project, you may need to search for other workarounds.
@OOPer thanks for you reply. The problem can be reproduced on iPhone 11 (iOS 14.1) and iPhone 7 Plus (iOS 14.0.1) and not on simulators. I am using Xcode 12.1. Unfortunately moving sheet out of out of NavigationLink is not an option for me.
I had exactly the same issue with my iPhone (v14.1). But It's fixed since version 14.2 (without changing anything in code)

@OOPer moving .sheet out of NavigationLink worked for me - Thanks a lot (was about to give up otherwise ;-) !!!!
SwiftUI sheet gets dismissed the first time it is presented
 
 
Q