SwiftUI sheet automatically dismisses itself

I have a sheet thats being presented in SwiftUI and the first time it's opened it automatically dismisses itself half a second later. Reopening it from then on works properly. Has anyone else experienced this and perhaps come up with a solution?

Stack Overflow post: https://stackoverflow.com/questions/62722308/swiftui-sheet-automatically-dismisses-itself

Code Block class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView:
                                                                NavigationView{
                                                                    List{
                                                                        ForEach(0..<10){ value in
                                                                            Text("Hello, World \(value)")
                                                                        }
                                                                    }
                                                                    NavigationLink(destination: Test()) {
                                                                        Text("Details")
                                                                    }
                                                                }
            )
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}
struct Test : View{
    @State  var show = false
    
    var body : some View{
        Text("Details")
            .sheet(isPresented: $show, content: {
                Color.blue
            })
            .toolbar{
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Show Sheet"){
                        show.toggle()
                    }
                }
            }
    }
}


Post not yet marked as solved Up vote post of rspoon3 Down vote post of rspoon3
5.4k views

Replies

I am experiencing this problem myself. Did you ever figure this out?
Same here, and I still could not figure out the reason. Did you?
It could be related to the fact that we are using a sheet modal inside a navigationlink view (although this does not explain why it happens)

PS: your link to stackoverflow seems to be broken
I'm experiencing this, too. Seems to be in the last month or so, and it's inconsistent. SwiftUI bug? Happens sometimes but not others. Would love an update if anyone figures it out.

Here's a similar stackoverflow question, but no answer:
https://stackoverflow.com/questions/64298618/swiftui-modal-sheet-dismisses-itself-after-half-a-second
It's 2021, and this is still an issue.

I had the same issue and the below code solution worked for me. Basically, all I did was put the .onTapGesture code that enables the sheet to display below the sheet code instead of above it. I know this looks very similar to the problem statement, but maybe the difference was setting the variable to true instead instead of toggling it. Not 100% sure.

At least for me, after I did that, the sheet didn't do the weird disappearance thing after I opened it the first time.

         VStack(alignment: .center) {
          Image(systemName: "info.circle")
            .font(.system(size: 22))
            .foregroundColor(.blue)
            .sheet(isPresented: $showFoodDataSheet) {
              FoodHistorySheet(foodRetriever: item, showFoodDataSheet: $showFoodDataSheet)
            }
            .onTapGesture {
              showFoodDataSheet = true
            }
        }
  • I had a similar issue. All I tried was to remove the dismiss @Environment line "@Environment(.dismiss) var dismiss", and the issue went away.

  • In my case it was the @Environment(.presentationMode) var presentation, removing it solved the issue. Thanks stockSmith!

Add a Comment

I am in the exact same situation right now... Why are there no answers after a year?

I fixed it by not using UIViewController on the page. Don't know that was the exact problem though since the view was dismissing at seeming completely unrelated timing.