【问题标题】:How do you display a view (in ContentView) depending on a variable of another view in SwiftUI?如何根据 SwiftUI 中另一个视图的变量显示一个视图(在 ContentView 中)?
【发布时间】:2020-05-08 17:59:20
【问题描述】:

基本上,在我的 ContentView 中,当 Binding variableName(不是 ContentView 中的 variableName,来自 MapView 类的 variableName)为真时,我试图创建一个 CreateItemButton()。所以.... ContentView 的伪代码类似于:

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.
            }
        }
    }
}

我希望有一些函数可以在 MapView() 视图上调用以选择性地显示 CreateEventButton() 视图,但我发现最接近我需要的是 .sheet(isPresented: $variableName) 函数....而且我需要将视图直接显示在地图顶部,而不是拉出带有视图的新工作表。让我知道是否可以添加任何内容以帮助使其更清晰!

【问题讨论】:

    标签: view binding swiftui mapbox


    【解决方案1】:

    在这里

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2020-06-21
      • 2010-12-21
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多