SwiftUI show/hide title issues with NavigationBar

后端 未结 1 1840
甜味超标
甜味超标 2020-12-21 14:57

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

//Main View
struct ContentView: View {

    var body: some View {
        NavigationView         


        
相关标签:
1条回答
  • 2020-12-21 15:34

    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

    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
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题