How do you display a view (in ContentView) depending on a variable of another view in SwiftUI?

Viewed 58

Basically, in my ContentView, I'm trying to make a CreateItemButton() when the Binding variableName (not the variableName in ContentView, the variableName from the MapView class) is true. So....the pseudocode for ContentView looks something like:

struct ContentView: View {
    @State private var variableName: Bool = false
    var body: some View {
        VStack {
            ZStack {
                MapView(variableName: $variableName)
                //Display the CreateEventButton() view here if $variableName is true.
            }
        }
    }
}

I was hoping there was either some function that I can call on the MapView() view to optionally display the CreateEventButton() view but the closest thing I found to what I need was the .sheet(isPresented: $variableName) function....and I need the view to display directly on top of the map, not pull up a new sheet with the view on it. Let me know if I can add anything to help make it clearer!

1 Answers

Here it is

ZStack {
    MapView(variableName: $variableName)
    if variableName {
         CreateEventButton()
    }
}
Related