如何在 SwiftUI 的标签栏特定视图中隐藏导航栏? [英] How do I hide navigation bar in the tab bar's specific view in SwiftUI?

查看:31
本文介绍了如何在 SwiftUI 的标签栏特定视图中隐藏导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XCode11 beta3,MacOS Catalina 10.15 Beta(19A501i)

XCode11 beta3, MacOS Catalina 10.15 Beta(19A501i)

我想在push时隐藏tabBar~任何命令都会很有帮助,谢谢~

I want to hide tabBar when push~ Any command will very helpful, Thanks~

点击我显示 gif 图片:

struct ContentView : View {
    var body: some View {
        WhenNavigationViewIsRootView()
    }
}

struct WhenNavigationViewIsRootView : View {
    var body: some View {
        NavigationView {
            TabbedView{
                Rectangle().foregroundColor(.green)
                    .tag(0).tabItem{Text("Page1")}

                VStack {
                    List {
                        ForEach(0...2) { i in
                            NavigationLink(
                                destination: Text("(i)"),
                                label: {Text("(i)")})
                        }
                    }
                }.tag(1).tabItem{Text("Page2")}
            }
            .navigationBarHidden(true)
        }
    }
}

推荐答案

如果要在 TabbedView 中隐藏导航栏,必须设置 .navigationBarHidden(true) 嵌套在 TabbedView 中的视图上.然而,这还不够.无论出于何种原因,SwiftUI 都要求您先设置导航栏标题,然后才能隐藏导航栏.

If you want to hide the navigation bar in a TabbedView, you have to set .navigationBarHidden(true) on the views nested inside TabbedView. This isn't enough, however. For whatever reason, SwiftUI requires that you first set the navigation bar title before you can hide the navigation bar.

NavigationView {
    TabbedView{
        Rectangle()
            .foregroundColor(.green)
            .tag(0)
            .tabItem{
                Text("Page1")
            }
            .navigationBarTitle("")
            .navigationBarHidden(true)

        List(0...2) { i in
            NavigationLink(destination: Text("(i)")) {
                Text("(i)")
            }
        }
        .tag(1)
        .tabItem {
            Text("Page2")
        }
        .navigationBarTitle("")
        .navigationBarHidden(true)
    }
}

这篇关于如何在 SwiftUI 的标签栏特定视图中隐藏导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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