简体   繁体   English

创建没有返回按钮的 NavigationLink SwiftUI

[英]Create a NavigationLink without back button 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.我试图链接 SomeView1() 中的按钮操作以导航到 someView2() 而没有屏幕顶部的后退按钮。 Instead, I want to add another button in SomeView2() that will navigate back to SomeView1().相反,我想在 SomeView2() 中添加另一个按钮,该按钮将导航回 SomeView1()。 is this possible in SwiftUI yet?这在 SwiftUI 中可能吗?

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

this is what it looks like:这是它的样子: 在此处输入图像描述

The right way to get what you want here is to use the presentationMode environment variable:在这里得到你想要的东西的正确方法是使用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()
    }
}

You can do something like this in SomeView2() :您可以在SomeView2()中执行以下操作:

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

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

I believe that you should use only one NavigationView for the whole navigation process.我相信你应该在整个导航过程中只使用一个 NavigationView。 Now you have three NavigationViews inside each other, which produces three back buttons.现在,您在彼此内部拥有了三个 NavigationView,这会产生三个后退按钮。

So in your case it would become something like this:所以在你的情况下,它会变成这样:

struct SomeView1InsideNavigationView: View { // This should be the first view you present
    var body: some View {
        NavigationView { // Use NavigationView only once
            SomeView1()
        }
    }
}

struct SomeView1: View {
    var body: some View {
        VStack { // Do *not* use NavigationView here
            //...view's content

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

struct SomeView2: View {
    var body: some View {
        VStack { // Do *not* use NavigationView here
            //...view's content

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI 带导航链接的消失后退按钮 - SwiftUI disappear back button with navigationLink 在 SwiftUI NavigationView 上显示系统后退按钮,之前没有 NavigationLink - Display system back button on SwiftUI NavigationView with no previous NavigationLink 在 SwiftUI 中单击按钮上的 NavigationView 和 NavigationLink? - NavigationView and NavigationLink on button click in SwiftUI? 在没有 NavigationView 的 SwiftUI 中创建 NavigationLink - Creating a NavigationLink in SwiftUI without NavigationView SwiftUI:如何更改警报按钮和 NavigationLink 后退按钮的颜色? - SwiftUI: How can I change the Color of Alert Button and NavigationLink back button? SwiftUI 按钮在 NavigationLink 项目区域内无效 - SwiftUI button inactive inside NavigationLink item area SwiftUI:无需返回按钮即可导航到视图 - SwiftUI: navigate to a view without getting the back button SwiftUI 列出不带 NavigationLink 的披露指示器 - SwiftUI List disclosure indicator without NavigationLink 如何从 SwiftUI 中的形状创建 NavigationLink 或按钮,仅通过点击形状而不是其框架来触发 - How to create a NavigationLink or Button from a shape in SwiftUI that is only triggered by tapping on the shape but not its frame 从目标视图返回时,SwiftUI NavigationLink 显示为突出显示 - SwiftUI NavigationLink shows as highlighted when going back from destination view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM