SwiftUI 使用导航链接从模式表过渡到常规视图 [英] SwiftUI transition from modal sheet to regular view with Navigation Link

查看:78
本文介绍了SwiftUI 使用导航链接从模式表过渡到常规视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SwiftUI,并且有一个起始页面.当用户按下此页面上的按钮时,会弹出一个模式表.

I'm working with SwiftUI and I have a starting page. When a user presses a button on this page, a modal sheet pops up.

在模式表旁边,我有一些这样的代码:

In side the modal sheet, I have some code like this:

  NavigationLink(destination: NextView(), tag: 2, selection: $tag) {
            EmptyView()
    }

我的模态工作表视图包含在导航视图中.

and my modal sheet view is wrapped inside of a Navigation View.

当 tag 的值变为 2 时,视图确实转到 NextView(),但它也显示为用户可以向下滑动的模态表,我不想要这样.

When the value of tag becomes 2, the view does indeed go to NextView(), but it's also presented as a modal sheet that the user can swipe down from, and I don't want this.

我想从模式表转换为常规视图.

I'd like to transition from a modal sheet to a regular view.

这可能吗?我试过隐藏导航栏等,但似乎没有什么区别.

Is this possible? I've tried hiding the navigation bar, etc. but it doesn't seem to make a difference.

对此事的任何帮助将不胜感激.

Any help with this matter would be appreciated.

推荐答案

您可以通过创建 environmentObject 并将 navigationLink 目标值绑定到 来实现此目的environmentObject 的值然后在模态视图中更改 environmentObject 的值.

You can do this by creating an environmentObject and bind the navigationLink destination value to the environmentObject's value then change the value of the environmentObject in the modal view.

这是一个解释我的意思的代码

Here is a code explaining what I mean

import SwiftUI

class NavigationManager: ObservableObject{
    @Published private(set) var dest: AnyView? = nil
    @Published var isActive: Bool = false

    func move(to: AnyView) {
        self.dest = to
        self.isActive = true
    }
}

struct StackOverflow6: View {
    @State var showModal: Bool = false
    @EnvironmentObject var navigationManager: NavigationManager
    var body: some View {
        NavigationView {
            ZStack {
                NavigationLink(destination: self.navigationManager.dest, isActive: self.$navigationManager.isActive) {
                    EmptyView()
                }

                Button(action: {
                    self.showModal.toggle()
                }) {
                    Text("Show Modal")
                }
            }
        }
            .sheet(isPresented: self.$showModal) {
                secondView(isPresented: self.$showModal).environmentObject(self.navigationManager)
            }
    }
}

struct StackOverflow6_Previews: PreviewProvider {
    static var previews: some View {
        StackOverflow6().environmentObject(NavigationManager())
    }
}


struct secondView: View {
    @EnvironmentObject var navigationManager: NavigationManager
    @Binding var isPresented: Bool
    @State var dest: AnyView? = nil

    var body: some View {
        VStack {
            Text("Modal view")
            Button(action: {
                self.isPresented = false
                self.dest = AnyView(thirdView())
            }) {
                Text("Press me to navigate")
            }
        }
        .onDisappear {
            // This code can run any where but I placed it in `.onDisappear` so you can see the animation
            if let dest = self.dest {
                self.navigationManager.move(to: dest)
            }
        }
    }
}

struct thirdView: View {
    var body: some View {
        Text("3rd")
            .navigationBarTitle(Text("3rd View"))
    }
}

希望这对您有所帮助,如果您对此代码有任何疑问,请告诉我.

Hope this helps, if you have any questions regarding this code, please let me know.

这篇关于SwiftUI 使用导航链接从模式表过渡到常规视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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