SwiftUI: Sheet gets dismissed immediately after being presented

2.9k views Asked by At

I want to have a fullscreen SwiftUI View with a button in the Navigation Bar, which presents a SwiftUI Sheet above.

Unfortunately, the Compiler says: "Currently, only presenting a single sheet is supported. The next sheet will be presented when the currently presented sheet gets dismissed."

This is my Code:

struct ContentView: View {
    var body: some View {
        EmptyView().fullScreenCover(isPresented: .constant(true), content: {
            FullScreenView.init()
        })
    }
}

struct FullScreenView: View{
    var body: some View {
        NavigationView{
            MasterView()
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
}

struct MasterView: View {
    @State private var showingSheet = false
     var body: some View {
            Form {
                Section(header: Text("Header")) {
                    NavigationLink(destination: UIKitView()) { Text("Hey") }
                }
            }
            .navigationBarItems(trailing:
             HStack {
                // First Try: Use a Button
                Button("Plus"){
                    showingSheet = true
                }.sheet(isPresented: $showingSheet){
                    AddContentView()
                }
                // Second Try: Use NavigationLink
                NavigationLink(
                    destination: AddContentView(),
                    label: {
                        Image(systemName: "plus.square.fill")
                    })
             })
    }
}

The Problem

I want to show the SwiftUI View in Fullscreen, so I use fullScreenCover(...). With this first "Sheet", I cannot present a second sheet, my AddContentView() View. Is there any way how I can fix this? I really want to have this sheet above :(

Thanks for any help!!

Feel free to ask for other code or if there are ambiguities. :)

1

There are 1 answers

0
Taeeun Kim On

The error message says that the sheet cannot be displayed at the same time(Do not overlap sheets), so if you want to go to view and again to another view, you have to use a NavigationLink and only at the end .sheet()

.sheet(isPresented: $showingSheet){
    AddContentView()
}

or fullScreenCover()

.fullScreenCover(isPresented: $showingSheet){
    AddContentView()
}

Edited: Sheet is not overlapped twice in this code.

import SwiftUI

struct ContentView: View {
    @State private var showingSheet = false
    var body: some View {
        NavigationView{
            Form {
                Section(header: Text("Header")) {
                    NavigationLink(destination: EmptyView()) { Text("Hey") }
                }
            }
            .navigationBarItems(trailing:
             HStack {
                // First Try: Use a Button
                Button("Plus"){
                    showingSheet = true
                }.sheet(isPresented: $showingSheet){
                    EmptyView()
                }
                // Second Try: Use NavigationLink
                NavigationLink(
                    destination: EmptyView(),
                    label: {
                        Image(systemName: "plus.square.fill")
                    })
             })

        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
}

p.s. fullScreenCover() belongs to the sheet