【问题标题】:SwiftUI Popover Disappears When It's Inside a NavigationViewSwiftUI 弹出框在 NavigationView 中时消失
【发布时间】:2020-10-01 23:53:23
【问题描述】:

我在 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 中的错误吗?有什么解决方法吗?

【问题讨论】:

  • Xcode 12 / iOS 14 无法重现。
  • 我也在 Xcode 12 / iOS 14 上,也许它只在 iPad 上。

标签: ios swift swiftui


【解决方案1】:

改用工作表:

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")
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2022-11-21
    • 2021-01-15
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多