SwiftUI 显示/隐藏 NavigationBar 的标题问题 [英] SwiftUI show/hide title issues with NavigationBar

查看:43
本文介绍了SwiftUI 显示/隐藏 NavigationBar 的标题问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码结构,这给我带来了很多麻烦:

I have the following code construct which gives me a lot of trouble:

//Main View
struct ContentView: View {

    var body: some View {
        NavigationView{
            ZStack(alignment: .center){
                CarouselBuilder()
                ProfileInvoke().navigationBarTitle("").navigationBarHidden(true)
            }
        }
    }
}

//Carousel filled with Cards from a DB 
...code irrelevant for my problem

//Profile Invoke -> Invokes a slide out menu called Menu that has NavigationLinks in it
struct Menu: View {
    var body: some View {
        ZStack{
            VStack(alignment: .center){
                    MenuButton(buttonText: "Settings", buttonCallView: AnyView(SettingsView() ))
                    MenuButton(buttonText: "My Favourites", buttonCallView: AnyView(MyFavouritesView()))
                    MenuButton(buttonText: "Sign Out", buttonCallView: AnyView(SignOutView()))
            }.frame(width: UIScreen.main.bounds.width/1.2,alignment: .top)
        }
    }
}

//MenuButtons are basic NavigationLinks linking to certain Views given as argument when calling them

我现在将主视图中的 ZStack 包装在一个 NavigationView 中,我需要这样做才能使 NavigationLinks 工作.我还必须在这个顶级"级别上执行此操作,因为我需要由滑出菜单中的链接调用的新视图,以获取整个屏幕,而不仅仅是显示滑出视图的宽度.

I now do wrap the ZStack in the Main View in a NavigationView which I need to to in order for the NavigationLinks to work. I also have to do this on this "top" level as I need the new View that will be invoked by the links in the slide out menu to take the entire screen and not only the width the slide out view is being displayed.

我现在的问题是,我当然不希望导航栏占据主视图中的空间.为此,我将其隐藏属性设置为 true.这会贯穿整个应用程序,并且还会禁用菜单中按钮链接的子视图中的导航视图.这让我无法回头.

My issue is now that I certainly do not want the navigation bar to take up space in the main view. For this I set the hidden attribute for it to true. This tho, carries through the entire app and also disables the navigation view in the subviews linked to by the buttons in the menu. Which gives me no way of going back.

我的问题是:1)有没有更优雅的方式来做这一切?2) 如何在子视图中重新调用导航栏?(将它们的隐藏导航栏属性设置回 false 不起作用.

My question would be: 1) Is there a more elegant way of doing all of this? 2) How can I re-invoke the navigation bar in sub views? (Setting the hidden navigation bar attribute on them back to false did not work.

推荐答案

以下是在根视图中隐藏导航栏并在子子视图中显示的可能方法.唯一需要的修改是在根视图中.

Below is a possible approach to hide navigation bar in root view and show in child subviews. The only needed modifications is in root view.

使用 Xcode 11.4/iOS 13.4 测试

Tested with Xcode 11.4 / iOS 13.4

这里只是一个根,子子视图是常规的,对于这种情况不需要特殊代码.内联查看重要注释.

Here is a root only, child sub-views are regular and do not require special code for this case. See important notes inline.

struct RootNavigationView: View {
    @State private var hideBar = true // << track hide state, and default
    var body: some View {
        NavigationView {
            VStack {
                Text("I'm ROOT")
                Divider()
                NavigationLink("Goto Child", destination: NextChildView(index: 1))
                 .simultaneousGesture(TapGesture().onEnded {
                    self.hideBar = false     // << show, here to be smooth !!
                 })
            }
            .navigationBarHidden(hideBar)
        //    .navigationBarTitle("Back to Root") // << optional 
            .onAppear {
                self.hideBar = true  // << hide on back
            }
        }
    }
}

这篇关于SwiftUI 显示/隐藏 NavigationBar 的标题问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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