在没有NavigationView的SwiftUI中创建NavigationLink

时间:2020-11-05 12:23:50

标签: ios swift swiftui swiftui-navigationlink

因此,我found这段了不起的代码,它使用presentationMode环境变量来存档没有NavigationView的视图之间的过渡:

import SwiftUI

struct View2: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    var body: some View {
        VStack {
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("POP")
            }
        }
        .navigationBarTitle("")
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: View2()) {
                Text("PUSH")
                    .navigationBarTitle("")
                    .navigationBarHidden(true)
            }
        }
    }
}

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

但是当我将其包装在另一个视图中时(这是我需要在自己的应用中执行的操作):

import SwiftUI

struct View2: View {
    @SwiftUI.Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    var body: some View {
        VStack {
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("POP")
            }
        }
        .navigationBarTitle("")
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
    }
}

struct Playground: View {
    var index: Int
    var body: some View {
        NavigationView {
            NavigationLink(destination: View2()) {
                Text("PUSH #\(index)")
                    .navigationBarTitle("")
                    .navigationBarHidden(true)
            }
        }
    }
}

struct Playgrounds: View {
    var body: some View {
        VStack {
            Text("List of bush buttons").font(.title)
            List(0..<5, id: \.self) { index in
                HStack {
                    Playground(index: index)
                }
            }
        }
    }
}

struct Playground_Previews: PreviewProvider {
    static var previews: some View {
        Playgrounds()
    }
}

我只能看到“后退”按钮的列表(没有标签!): enter image description here 而不是“ Push#1”,“ Push#2”等列表(应打开View2 onClick事件)。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您只需要一个NavigationView,而不是为每个NavigationView定义一个NavigationLink

struct Playground: View {
    var index: Int
    var body: some View {
        NavigationLink(destination: View2()) {
            Text("PUSH #\(index)")
        }
    }
}

struct Playgrounds: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("List of bush buttons").font(.title)
                List(0..<5, id: \.self) { index in
                    HStack {
                        Playground(index: index)
                    }
                }
            }
            .navigationBarTitle("")
            .navigationBarHidden(true)
        }
    }
}