登录后的 SwiftUI 导航 [英] SwiftUI Navigation after login

查看:28
本文介绍了登录后的 SwiftUI 导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我准备了 2 个签名视图和主视图.在用户登录主视图后,我尝试弹出/隐藏签名视图.

I have prepared 2 views which are signing and also the home view. I tried to pop/hide the signing view after the user signing to the home view.

现在的问题是视图允许用户单击后退按钮返回登录视图.我不知道如何解决这个问题.谁能给我一些提示?

The problem now is the view is allow the user to click the back button to go back to the login view. I have no idea how to settle this. Can anyone give me some hints?

这是我的登录导航代码:

Here is my navigation code for login:

NavigationLink(destination: HomePageView(), tag: 1, selection: $selection) {
    Button(action: {
        print("Register tapped")
        self.verify()
        self.selection = 1
    }) {
        HStack {
            Text("OK").foregroundColor(Color.white).bold().foregroundColor(.white)
                .frame(width: UIScreen.main.bounds.width - 30, height: UIScreen.main.bounds.height / 12)
                .background(Color.orange)
                .cornerRadius(35.0)
                .font(.headline)
                .padding()
        }
    }
}

推荐答案

每个导航堆栈应该只有一个 NavigationView.您需要删除除顶部的所有嵌套 NavigationView.在您的子视图中,您仍然可以修改顶部的视图.

There should only be one NavigationView per navigation stack. You need to remove all nested NavigationViews except the top one. In your child views you still can modify the top one.

这是一个简单的演示:

struct LoginView: View {
    @State private var active: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: HomeView(), isActive: self.$active) {
                    Text("Register")
                }
            }
            .navigationBarTitle("Login View")
        }
    }
}

struct HomeView: View {
    var body: some View {
        Text("inside home view")
            .navigationBarTitle("Home view")
            .navigationBarBackButtonHidden(true)
    }
}

在 Xcode 11.6、iOS 13.6 中测试.

Tested in Xcode 11.6, iOS 13.6.

这篇关于登录后的 SwiftUI 导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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