【问题标题】:SwiftUI - NavigationBar not showing in Modal NavigationViewSwiftUI - 导航栏未显示在模态导航视图中
【发布时间】:2019-11-07 20:50:23
【问题描述】:

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

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

模态显示成功,但我无法在模态中显示 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
        })

    }
}

应用程序构建,并显示窗体,但 NavigationView 没有:

如何制作这个节目?或者我应该使用另一个视图来代替 NavigationView

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您需要将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
                })
           }
        }
    }
    

    HackingWithSwift 的 article 显示了正确的位置。

    【讨论】:

    • 谢谢,马上解决了!
    • @Andrew 为什么需要将 NavigationView 的修饰符放在 NavigationView 中?请参阅Text,我们正在对其进行修饰,例如lineLimitfontNavigationViewText 都继承自基类,即 View,为什么会有区别?任何你可以给我看的理由或文章。
    • 来自苹果自己的tutorial 表明它应该在里面。这个medium article 给出了一个相当详细的解释,说明它为什么存在。
    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多