简体   繁体   English

SwiftUI - TabView 内的 OnExitCommand

[英]SwiftUI - OnExitCommand inside TabView

I have lately been trying to make a tvOS app, but have run into the following rather annoying problem.我最近一直在尝试制作一个 tvOS 应用程序,但遇到了以下相当烦人的问题。 I can't use navigation inside a TabView and still have the menu button on the remove take me back to the previous state.我不能在 TabView 中使用导航,并且仍然有删除菜单按钮将我带回到以前的 state。

struct TestView: View {
    
    @State var selection : Int = 0
    
    var body: some View {
        TabView(selection: self.$selection) {
            ExpView()
            .tabItem {
                HStack {
                    Image(systemName: "magnifyingglass")
                    Text("Explore")
                }
            }
            .tag(0)
        }
    }
}

struct ExpView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView(title: "Hey")) {
                Text("Detail")
            }
        }
    }
}

struct DetailView: View {
    var title : String
    var body: some View {
        VStack {
            Text(title)
        }
    }
}

My question is: Is there any way to enable the menu button to go back to the previous view in the hierachy without dismissing the app completely?我的问题是:有什么方法可以在不完全关闭应用程序的情况下启用 go 的菜单按钮回到层次结构中的上一个视图?

You don't need to call dismiss on Menu it is called automatically for NavigationLink (so calling one more dismiss quits to main menu)您不需要在 Menu 上调用dismiss,它会为 NavigationLink 自动调用(因此再调用一次dismiss退出到主菜单)

Here are fixed views.这里是固定视图。 Tested with Xcode 11.4用 Xcode 11.4 测试

struct ExploreView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView(title: "Hey")) {
                Text("Detail")
            }
        }
    }
}

struct DetailView: View {

    var title : String
    var body: some View {
        VStack {
            Text(title)
        }
    }
}

So I found a workaround for the issue.所以我找到了解决这个问题的方法。 If you place the navigationView outside the TabView and then use the following code it works:如果将 navigationView 放在 TabView 之外,然后使用以下代码,它可以工作:

struct TestView: View {
    
    @State var selection : Int = 0
    @State var hideNavigationBar : Bool
    
    var body: some View {
        NavigationView {
            TabView(selection: self.$selection) {
                ExpView(hideNavigationBar: self.$hideNavigationBar)
                .tabItem {
                    HStack {
                        Image(systemName: "magnifyingglass")
                        Text("Explore")
                    }
                }
                .tag(0)
            }
        }
    }
}

struct ExpView: View {
    
    @Binding var hideNavigationBar : Bool
    
    var body: some View {
        NavigationLink(destination: DetailView(title: "Hey")) {
            Text("Detail")
        }.navigationBarTitle("")
        .navigationBarHidden(self.hideNavigationBar)
        .onAppear {
            self.hideNavigationBar = true
        }
    }
}

struct DetailView: View {
    var title : String
    var body: some View {
        VStack {
            Text(title)
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM