创建没有后退按钮 SwiftUI 的 NavigationLink [英] Create a NavigationLink without back button SwiftUI

查看:15
本文介绍了创建没有后退按钮 SwiftUI 的 NavigationLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图链接 SomeView1() 中的按钮操作以导航到 someView2() 而不在屏幕顶部有后退按钮.相反,我想在 SomeView2() 中添加另一个按钮,该按钮将导航回 SomeView1().这在 SwiftUI 中是否可行?

Im trying to link a button action in SomeView1() to navigate to a someView2() without having the back button at the top of the screen. Instead, I want to add another button in SomeView2() that will navigate back to SomeView1(). is this possible in SwiftUI yet?

SomeView1()

SomeView1()

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

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

SomeView2()

SomeView2()

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

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

这就是它的样子:

推荐答案

这里得到你想要的正确方法是使用 presentationMode 环境变量:

The right way to get what you want here is to use the presentationMode environment variable:

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

这篇关于创建没有后退按钮 SwiftUI 的 NavigationLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆