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