How to remove the default Navigation Bar space in SwiftUI NavigiationView

后端 未结 15 2745
我在风中等你
我在风中等你 2020-11-29 20:00

I am new to SwiftUI (like most people) and trying to figure out how to remove some whitespace above a List that I embedded in a NavigationView

In this image, you can

15条回答
  •  天命终不由人
    2020-11-29 20:05

    Similar to the answer by @graycampbell but a little simpler:

    struct YourView: View {
    
        @State private var isNavigationBarHidden = true
    
        var body: some View {
            NavigationView {
                VStack {
                    Text("This is the master view")
                    NavigationLink("Details", destination: Text("These are the details"))
                }
                    .navigationBarHidden(isNavigationBarHidden)
                    .navigationBarTitle("Master")
                    .onAppear {
                        self.isNavigationBarHidden = true
                    }
                    .onDisappear {
                        self.isNavigationBarHidden = false
                    }
            }
        }
    }
    

    Setting the title is necessary since it is shown next to the back button in the views you navigate to.

提交回复
热议问题