简体   繁体   中英

SwiftUI: Hide Navigation Bar on Specific Screens

It seems as though I'm unable to choose which pages I want to hide the Navigation Bar on in swift UI.

I have the following screens:

Main

struct Main: View {
    var body: some View {
        NavigationView {
                Home()
            }
        }
}

Home

struct Home: View {
    var body: some View {
            NavigationLink(destination: Details()) {
                    Text("Go Next")
            }
        // I expect the navigation bar to show up here, and it does
        .navigationBarTitle("Home")
        .navigationBarHidden(false)
    }
}

Details

struct Details: View {
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    var body: some View {
            Button(action: { self.mode.wrappedValue.dismiss() }) {
                Text("Go Back")
            }
        // I expect the navigation bar to be hidden here
        // At first, it is hidden. Then, after a second, it pops up 
        // with the title "Details"
        .navigationBarTitle("Details")
        .navigationBarHidden(true)
    }
}

Either I'm doing this wrong (likely), or Apple has some work to do (also likely).

As I saw earlier , something comes wrong, when you use both:

.navigationBarTitle("some text")
.navigationBarHidden(true)

in code below there is no navigation bar staff and nothing pops up:

struct Details: View {
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    var body: some View {
            Button(action: { self.mode.wrappedValue.dismiss() }) {
                Text("Go Back")
            }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true) // even no back button
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM