bar Items 中的导航链接返回到 NavigationView 的顶部 [英] Navigation Link in bar Items goes back to top of NavigationView

查看:54
本文介绍了bar Items 中的导航链接返回到 NavigationView 的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct Testing: View {

    var body: some View {
        NavigationView{
            VStack {
                Text("View 1")
                Text("View 1.3")
            
                NavigationLink(destination: TestView(),
                                    label: {
                                        Text("View 1 navigation")
                                    })
            }
        }
    }
}

struct TestView: View {

    var body: some View {
            VStack {
                Text("View 2")
            }
            .navigationBarItems(trailing:NavigationLink(
                                    destination: Text("View 3"),
                                    label: {
                                        Text("Navigate")
                                     }))
    }
}

当单击 TestViews navigationBarItems 内的 NavigationLink 时,它导航到视图 3.当我单击后退按钮时,它不会返回到 TestView,它会一直返回改为测试.

When clicking the NavigationLink inside of TestViews navigationBarItems it navigates to View 3. When I click the back button it does not go back to TestView it goes all the way back to Testing instead.

如何让它回到上一个视图而不是第一个视图?

How do I make it go back to the previous view instead of the first view?

我使用的是 Xcode 12.

I am using Xcode 12.

推荐答案

将导航链接放入导航视图主体(栏不在导航视图中)

Put navigation link into body of navigation view (bar is not in navigation view)

所以这是可能的方法

struct TestView: View {
    @State private var isActive = false

    var body: some View {
            VStack {
                Text("View 2")
            }
            .background(
               NavigationLink(destination: Text("View 3"), isActive: $isActive,
                label: { EmptyView() })
            )
            .navigationBarItems(trailing:
               Button("Navigate", action: { self.isActive = true }))
    }
}

这篇关于bar Items 中的导航链接返回到 NavigationView 的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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