创建不带后退按钮SwiftUI的NavigationLink

时间:2019-10-25 16:03:26

标签: swift navigation swiftui

我试图在SomeView1()中链接按钮动作以导航到someView2(),而没有在屏幕顶部显示后退按钮。相反,我想在SomeView2()中添加另一个按钮,该按钮将导航回到SomeView1()。在SwiftUI中有可能吗?

SomeView1()

struct SomeView1: View {
    var body: some View {
        NavigationView {
            VStack {
                //...view's content

                NavigationLink(destination: SomeView2()) {
                    Text("go to SomeView2")
                }
                Spacer()
            }
        }
    }
}

SomeView2()

struct SomeView2: View {
    var body: some View {
        NavigationView {
            VStack {
                //...view's content

                NavigationLink(destination: SomeView1()) {
                    Text("go to SomeView1")
                }
                Spacer()
            }
        }
    }
}

是这样的: enter image description here

3 个答案:

答案 0 :(得分:2)

在这里获得所需内容的正确方法是使用presentationMode环境变量:

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()
    }
}

答案 1 :(得分:0)

您可以在SomeView2()中执行以下操作:

NavigationView {
   VStack {
            //...view's content

        NavigationLink(destination: SomeView1()) {
            Text("go to SomeView1")
        }
        Spacer()
    }
}.navigationBarBackButtonHidden(true)

答案 2 :(得分:-1)

我相信您应该在整个导航过程中仅使用一个NavigationView。现在,彼此之间具有三个NavigationViews,这将产生三个后退按钮。

因此,在您的情况下,它将变成这样:

const location = $('.info-list dt:contains("Location")').next().text();