首页 > 解决方案 > 模态表可以在 SwiftUI 中有导航栏吗?

问题描述

我在我的代码中从导航栏按钮呈现模式表:

struct MainPage : View {

    @State var isModalSheetShown: Bool = false

    var body: some View {
        VStack {
            [...]
        }
        .navigationBarItems(trailing: HStack {
            Button(action: { self.isModalSheetShown = true }) {
                Text("Add")
            }
        })
        .sheet(isPresented: $isModalSheetShown, content: {
            VStack {
                [...]
            }
            .navigationBarItems(trailing: HStack {
                Button(action: { ... }) {
                    Text("Done")
                })
            })
        })
    }
}

但是导航栏没有出现在模态表中,如下所示。

在此处输入图像描述

我在做什么错,你如何在模态表上放置导航栏?

标签: iosswiftswiftui

解决方案


你必须NaviagtionView像这样包装你的模态视图

@State var isModalSheetShown: Bool = false

var body: some View {
    VStack {
        Text("Main")
    }
    .navigationBarItems(trailing: Button("Add",
                                         action: { self.isModalSheetShown = true }))
    .sheet(isPresented: $isModalSheetShown) {
        NavigationView {
            VStack {
                Text("Modal")
            }
            .navigationBarItems(trailing: Button("Done",
                                                 action: {}))
        }
    }
}

推荐阅读