SwiftUI - NavigationBar 未显示在 Modal NavigationView 中

Spi*_*elo 2 ios swift swiftui

试图制作一个类似于 Apple 日历应用程序中的“创建事件”模式的模式。我在父 NavigationView 中使用以下代码成功显示了我的模式:

.navigationBarItems(trailing:
                    Button(action: {
                        self.isModal = true
                        }) {
                        Image(systemName: "plus").sheet(isPresented: $isModal, content: {
                            EventCreate(showModal: self.$isModal)
                        })
                    }
            )
Run Code Online (Sandbox Code Playgroud)

模态显示成功,但我无法在模态中显示 NavigationBar,如下所示:

struct EventCreate: View {

    @Binding var showModal: Bool

    @State var event = Event(id: 0, title: "", description: "", location: "", start: Date(), end: Date(), cancelled: false, public_occurrence: false, created: "", last_updated: "", guests: -1)

    var body: some View {
        NavigationView {
            Form{
                Section {
                    TextField("Title", text: $event.title)
                    TextField("Location", text: $event.location)
                }
                Section {
                    DatePicker(selection: $event.start, label: { Text("Start") })
                    DatePicker(selection: $event.end, label: { Text("End") })
                }
            }
        }
        .navigationBarTitle(Text("Create Event"), displayMode: .inline)
        .navigationBarItems(leading:
            Button("Close") {
                self.showModal = false
        })

    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序构建,并显示 Form,但 NavigationView 没有: 缺失视图的屏幕截图

我怎样才能制作这个节目?或者我应该使用另一种视图而不是 NavigationView

And*_*rew 6

你需要把navigationBarTitlenavigationBarItems修饰符放在里面NavigationView,而不是外面。这些修饰符必须放置在您嵌入的视图上,在您的情况下Form

struct EventCreate: View {

    @Binding var showModal: Bool

    @State var event = Event(id: 0, title: "", description: "", location: "", start: Date(), end: Date(), cancelled: false, public_occurrence: false, created: "", last_updated: "", guests: -1)

    var body: some View {
        NavigationView {
            Form{
                Section {
                    TextField("Title", text: $event.title)
                    TextField("Location", text: $event.location)
                }
                Section {
                    DatePicker(selection: $event.start, label: { Text("Start") })
                    DatePicker(selection: $event.end, label: { Text("End") })
                }
            }
            .navigationBarTitle(Text("Create Event"), displayMode: .inline)
            .navigationBarItems(leading:
            Button("Close") {
                self.showModal = false
            })
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

HackingWithSwift 的这篇文章显示了正确的位置。