当它位于NavigationView中时,SwiftUI Popover消失

时间:2020-10-01 23:53:23

标签: ios swift swiftui

我在NavigationView中有一个弹出框:

import SwiftUI

struct ContentView: View {
  var body: some View {
    NavigationView {
      NavigationLink(destination: ChildView()) {
        Text("Navigate")
      }
    }
    .navigationViewStyle(StackNavigationViewStyle())
  }
}

struct ChildView: View {
  @State private var popover = false

  var body: some View {
    HStack {
      Button(action: { self.popover = true }) {
        Text("Toggle")
      }
      .popover(isPresented: $popover) {
        Text("Yolo")
      }
    }
  }
}

启动应用程序后第一次切换弹出窗口时,它会立即消失。之后,它可以正常工作。这是NavigationView中的错误吗?有什么解决方法吗?

1 个答案:

答案 0 :(得分:0)

改为使用工作表:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: ChildView()
            ) {
                Text("Navigate")
            }
        }
    }
}

struct ChildView: View {
    @State
    private var isPresented = false
    var body: some View {
        HStack {
            Button(
                action: {
                    isPresented.toggle()
                }) {
                Text("Present")
            }
        }
        .sheet(
            isPresented: $isPresented
        ) {
            Text("Yolo")
        }
    }
}