I'm trying to create a menu in SwiftUI with the possibility of showing sheets, but when I try to display the menu the sheets doesn't display. Here's the code:
import SwiftUI
enum ActiveSheet: Identifiable {
case first, second
var id: Int {
hashValue
}
}
struct ContentView: View {
@State var activeSheet: ActiveSheet?
@State private var showingConfirmation = false
var body: some View {
Menu("Actions") {
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()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The actual result is this: enter image description here But without any possibility of access to the sheet pages I created. Please let me know!
Move the .sheet
to outside the Menu
:
struct ContentView: View {
@State var activeSheet: ActiveSheet?
@State private var showingConfirmation = false
var body: some View {
Menu("Actions") {
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()
}
}
}
}