How to remove the default Navigation Bar space in SwiftUI NavigiationView

后端 未结 15 2740
我在风中等你
我在风中等你 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:06

    I have had a similar problem when working on an app where a TabView should be displayed once the user is logged in.

    As @graycampbell suggested in his comment, a TabView should not be embedded in a NavigationView, or else the "blank space" will appear, even when using .navigationBarHidden(true)

    I used a ZStack to hide the NavigationView. Note that for this simple example, I use @State and @Binding to manage the UI visibility, but you may want to use something more complex such as an environment object.

    struct ContentView: View {
    
        @State var isHidden = false
    
        var body: some View {
            
            ZStack {
                if isHidden {
                    DetailView(isHidden: self.$isHidden)
                } else {
                    NavigationView {
                        Button("Log in"){
                            self.isHidden.toggle()
                        }
                        .navigationBarTitle("Login Page")
                    }
                }
            }
        }
    }
    

    When we press the Log In button, the initial page disappears, and the DetailView is loaded. The Login Page reappears when we toggle the Log Out button

    struct DetailView: View {
        
        @Binding var isHidden: Bool
        
        var body: some View {
            TabView{
                NavigationView {
                    Button("Log out"){
                        self.isHidden.toggle()
                    }
                    .navigationBarTitle("Home")
                }
                .tabItem {
                    Image(systemName: "star")
                    Text("One")
                }
            }
        }
    }
    

提交回复
热议问题