简体   繁体   English

带有 SwiftUI 的 macOS 应用程序中不稳定的导航视图

[英]Erratic navigation view in macOS app with SwiftUI

I'm trying to create a master/detail view with SwiftUI for a macOS app.我正在尝试使用 SwiftUI 为 macOS 应用程序创建主/细节视图。 The goal is to select an item in the side bar and have it change the main view accordingly.目标是在侧栏中选择一个项目并使其相应地更改主视图。 My code for this example is shown below:我的这个例子的代码如下所示:

import SwiftUI

struct MyMasterView: View {

    let names = ["Homer", "Marge", "Bart", "Lisa"]

    var body: some View {

        List {
            ForEach(names, id: \.self) { name in
                NavigationLink(name, destination: MyDetailView(name: name))
            }
        }
        .frame(width: 150, height: 300)

    }
}

struct MyDetailView: View {

    var name = "Name"

    var body: some View {

        HStack {
            Text("Hello \(name)")
                .font(.largeTitle)
        }
        .frame(width: 450, height: 300)

    }
}

struct ContentView: View {

    var body: some View {

        NavigationView {
            MyMasterView()
            MyDetailView()
        }
        .navigationViewStyle(DoubleColumnNavigationViewStyle())
        .frame(width: 600, height: 300)

    }
}

When running the Mac app, the side bar selections can become inactive and sometimes popover views appear instead of changing the detail view.运行 Mac 应用程序时,侧边栏选择可能会变为非活动状态,有时会出现弹出视图而不是更改详细信息视图。 See below for a screen capture video of the issue.请参阅下面的问题的屏幕截图视频。 Is this a bug with SwiftUI NavigationView on the Mac or is there something I need to implement to make this work on macOS?这是 Mac 上 SwiftUI NavigationView 的错误,还是我需要实现一些东西才能使其在 macOS 上工作?

https://youtu.be/BF7m1OszZ5w https://youtu.be/BF7m1OszZ5w

As of Xcode 11.0 (11A420a) and macOS Catalina (19A558d) NavigationView works as expected:从 Xcode 11.0 (11A420a) 和 macOS Catalina (19A558d) 开始,NavigationView 按预期工作:

import SwiftUI

struct DetailView: View {
    let text: String

    var body: some View {
        Text(text)
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

struct ContentView: View {
    private let names = ["Homer", "Marge", "Bart", "Lisa"]
    @State private var selection: String?

    var body: some View {
        NavigationView {
            List(selection: $selection) {
                Section(header: Text("The Simpsons")) {
                    ForEach(names, id: \.self) { name in
                        NavigationLink(destination: DetailView(text: name)) {
                            Text(name)
                        }
                    }
                }
            }.listStyle(SidebarListStyle())
            DetailView(text: "Make a selection")
        }
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM