【问题标题】:SwiftUI hiding a navigation bar only when looking at ContentViewSwiftUI 仅在查看 ContentView 时隐藏导航栏
【发布时间】:2020-06-03 22:29:20
【问题描述】:

我有一个内容文件并隐藏导航栏,因为它占用空间并将元素向下推。 ContentView 中的一个按钮重定向(使用导航链接)到另一个视图。在另一个视图中,navigationBar 仍然是隐藏的……为简单起见,我将从 ContentView 中删除一些代码:

//this is the view that looks "fine" (i.e. the navigation bar takes up no space)
struct ContentView: View {
    @State private var isPresentedSettings = false
    var body: some View {
        NavigationView {
            ZStack {
                VStack {
                    SettingsButton(isPresentedSettings: $isPresentedSettings)
                }
            }.navigationBarTitle("").navigationBarHidden(true)
        }
    }
}

//this is the button that pulls up the settings page view
struct SettingsButton: View {
    @Binding var isPresentedSettings: Bool
    var body: some View {
        NavigationLink (destination: SettingsPageView(isPresentedSettings: 
        self.$isPresentedSettings)) {
            Button(action: { self.isPresentedSettings.toggle() }, label: { Text("Button") })
        }
    }
}

//This is the view that should have a navigationbar but it doesn't
struct SettingsPageView: View {
    @Binding var isPresentedSettings: Bool
    var body: some View {
        NavigationView {
            VStack {
                Text("This is a view")
            }.navigationBarTitle("Settings", displayMode: .inline)
        }
    }
}

另外...可能有错别字,因为我刚刚从另一台计算机复制代码。抱歉,提前谢谢您!

【问题讨论】:

    标签: swift swiftui navigationbar navigationview


    【解决方案1】:

    首先,您不需要有这个isPresentedSettings 变量来呈现NavigationLink

    NavigationLink(destination: SettingsPageView()) {
        Text("Button")
    }
    

    您的视图层次结构中应该只有一个NavigationView

    这就是你的最终代码的样子:

    struct ContentView: View {
        @State private var navBarHidden = true
    
        var body: some View {
            NavigationView {
                ZStack {
                    VStack {
                        SettingsButton(navBarHidden: $navBarHidden)
                    }
                }
                .navigationBarHidden(navBarHidden)
            }
        }
    }
    
    struct SettingsButton: View {
        @Binding var navBarHidden: Bool
    
        var body: some View {
            NavigationLink(destination: SettingsPageView(navBarHidden: $navBarHidden)) {
                Text("Show View")
            }
        }
    }
    
    struct SettingsPageView: View {
        @Binding var navBarHidden: Bool
    
        var body: some View {
            VStack {
                Text("This is a view")
            }
            .navigationBarTitle("Settings", displayMode: .inline)
            .onAppear {
                self.navBarHidden = false
            }
            .onDisappear {
                self.navBarHidden = true
            }
        }
    }
    

    【讨论】:

    • 嗨,所以这段代码使得导航栏在我重定向到 SettingsPageView 时消失...我该如何解决这个问题?
    • 我在 Xcode 11.5、Swift 5.1 中测试了这段代码。 ContentView 屏幕没有导航栏,SettingsPageView 有。尝试仅使用我的代码,然后添加其他方法。
    • 嗯...navBar 应该隐藏在 ContentView 上(意味着您必须使用 .navigationBarHidden(true),而不是 false)...所以 navBar 隐藏在 ContentView 中,然后显示SettingsPageView 中的普通导航栏...
    • 哦,我的错。我更新了我的答案。现在应该可以工作了:)
    • 编辑:我认为这实际上会产生某种队列问题。当我按下设置按钮时,它会将 navBarHidden 设置为 true,这会很好地调出设置页面。但是,当我按下后退按钮时,可以看到明显的失真,因为导航栏从占用空间变为不占用空间......有什么办法可以避免这种情况?
    猜你喜欢
    • 2020-06-24
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2020-03-20
    相关资源
    最近更新 更多