我使用导航链接,我也想使用侧边菜单

I use navigationlink and I also want to use side menu

我的 iOS 应用中有一个使用 SwiftUI 构建的 NavigationView 组件。

我使用导航link 来改变我的 page.In 第二个屏幕,我想显示侧面 menu.But 现在我的 iOS 应用程序中有两层 NavigationView,它是我不想 show.I 希望第二个屏幕只有侧边菜单。

是否可能,或者我是否遗漏了一些有关 Swift 中导航管理方式的信息?

我应该如何更改我的代码?

这是我的 ContentView.swift:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView{
            VStack {
                NavigationLink(
                    destination: SwiftUIView_map(),
                    label: {
                        Text("Skip")
                            .fontWeight(.bold)
                            .font(.title)
                            .padding()
                            .background(Color.purple)
                            .cornerRadius(20)
                            .foregroundColor(.white)
                    })
                HeaderView()
                    .offset(y:-60)
                NavigationLink(
                    destination: Text("Login"),
                    label: {
                        Text("Login")
                            .fontWeight(.bold)
                            .font(.title)
                            .padding()
                            .background(Color.purple)
                            .cornerRadius(20)
                            .foregroundColor(.white)
                    })
                NavigationLink(
                    destination: Text("Sign up"),
                    label: {
                        Text("Sign up")
                            .fontWeight(.bold)
                            .font(.title)
                            .padding()
                            .background(Color.purple)
                            .cornerRadius(20)
                            .foregroundColor(.white)
                    })
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct HeaderView: View {
    var body: some View {
        HStack{
        VStack(alignment: .leading, spacing: 2){
            Text("Just")
                .font(.system(size: 60,weight:.heavy,design:.rounded))
                .fontWeight(.black)
                .multilineTextAlignment(.leading)
            Text("test")
                .font(.system(size: 60,weight:.heavy,design:.rounded))
                .fontWeight(.black)
        }
        Spacer()
        }
        .padding()
    }
}

导航侧边栏:

import SwiftUI

struct SwiftUIView_map: View {
    @State private var isShowing = false
    var body: some View {
        NavigationView{
            ZStack{
                if isShowing{
                    SideMenuView(isShowing: $isShowing)
                }
                HomeView()
                    .cornerRadius(isShowing ? 20 : 10 )
                    .offset(x: isShowing ? 300 : 0,y:isShowing ? 44 :0)
                    .scaleEffect(isShowing ? 0.8 : 1)
                    .navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.bullet")
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("home")
            }
        }
    }
}

struct SwiftUIView_map_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView_map()
    }
}

struct HomeView: View {
    var body: some View {
        ZStack{
            Color(.white)
            Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
                .padding()
        }
    }
}

如果需要更详细的代码,请点击link https://github.com/simpson0826/swifttest.git

您只需要在 ContentView(根视图)中使用 NavigationView

struct SwiftUIView_map: View {
    @State private var isShowing = false
    var body: some View {
//        NavigationView{
            ZStack{
                if isShowing{
                    SideMenuView(isShowing: $isShowing)
                }
                HomeView()
                    .cornerRadius(isShowing ? 20 : 10 )
                    .offset(x: isShowing ? 300 : 0,y:isShowing ? 44 :0)
                    .scaleEffect(isShowing ? 0.8 : 1)
                    .navigationBarItems(trailing: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.bullet")
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("home")
            }
//        }
    }
}