Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

ios - SwiftUI show/hide title issues with 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

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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Tested with Xcode 11.4 / iOS 13.4

demo

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
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...