Multiple sheet(isPresented:) doesn't work in SwiftUI Multiple sheet(isPresented:) doesn't work in SwiftUI swift swift

Multiple sheet(isPresented:) doesn't work in SwiftUI


UPD

Starting from Xcode 12.5.0 Beta 3 (3 March 2021) this question makes no sense anymore as it is possible now to have multiple .sheet(isPresented:) or .fullScreenCover(isPresented:) in a row and the code presented in the question will work just fine.

Nevertheless I find this answer still valid as it organizes the sheets very well and makes the code clean and much more readable - you have one source of truth instead of a couple of independent booleans

The actual answer

Best way to do it, which also works for iOS 14:

enum ActiveSheet: Identifiable {    case first, second        var id: Int {        hashValue    }}struct YourView: View {    @State var activeSheet: ActiveSheet?    var body: some View {        VStack {            Button {                activeSheet = .first            } label: {                Text("Activate first sheet")            }            Button {                activeSheet = .second            } label: {                Text("Activate second sheet")            }        }        .sheet(item: $activeSheet) { item in            switch item {            case .first:                FirstView()            case .second:                SecondView()            }        }    }}

Read more here: https://developer.apple.com/documentation/swiftui/view/sheet(item:ondismiss:content:)

To hide the sheet just set activeSheet = nil

Bonus:If you want your sheet to be fullscreen, then use the very same code, but instead of .sheet write .fullScreenCover


Please try below code

Update Answer (iOS 14, Xcode 12)

enum ActiveSheet {   case first, second   var id: Int {      hashValue   }}struct ContentView: View {    @State private var showSheet = false    @State private var activeSheet: ActiveSheet? = .first    var body: some View {            NavigationView {            VStack(spacing: 20) {                Button("First modal view") {                    self.showSheet = true                    self.activeSheet = .first                }                Button ("Second modal view") {                    self.showSheet = true                    self.activeSheet = .second                }            }            .navigationBarTitle(Text("Multiple modal view problem"), displayMode: .inline)            .sheet(isPresented: $showSheet) {                if self.activeSheet == .first {                    Text("First modal view")                }                else {                    Text("Only the second modal view works!")                }            }        }    }}


Can also add the sheet to an EmptyView placed in the view's background. This can be done multiple times:

  .background(EmptyView()        .sheet(isPresented: isPresented, content: content))