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 see that there is some white space above the List
What I want to accomplish is this
I've tried using
.navigationBarHidden(true)
but this did not make any noticeable changes.
i'm currently setting up my navigiationView like this
NavigationView {
FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
.navigationBarHidden(true)
}
where FileBrowserView is a view with a List and Cells defined like this
List {
Section(header: Text("Root")){
FileCell(name: "Test", fileType: "JPG",fileDesc: "Test number 1")
FileCell(name: "Test 2", fileType: "txt",fileDesc: "Test number 2")
FileCell(name: "test3", fileType: "fasta", fileDesc: "")
}
}
I do want to note that the ultimate goal here is that you will be able to click on these cells to navigate deeper into a file tree and thus should display a Back button on the bar on deeper navigation, but I do not want anything at the top as such during my initial view.
Try putting the
NavigationView
inside aGeometryReader
.I’ve experienced weird behavior when the
NavigationView
was the root view.Similar to the answer by @graycampbell but a little simpler:
Setting the title is necessary since it is shown next to the back button in the views you navigate to.
This is a bug present in SwiftUI (still as of Xcode 11.2.1). I wrote a
ViewModifier
to fix this, based on code from the existing answers:The purpose of a
NavigationView
is to add the navigation bar on top of your view. In iOS, there are 2 kinds of navigation bars: large and standard.If you want no navigation bar:
If you want a large navigation bar (generally used for your top-level views):
If you want a standard (inline) navigation bar (generally used for sub-level views):
Hope this answer will help you.
More information: Apple Documentation
For me, I was applying the
.navigationBarTitle
to theNavigationView
and not toList
was the culprit. This works for me on Xcode 11.2.1:For some reason, SwiftUI requires that you also set
.navigationBarTitle
for.navigationBarHidden
to work properly.Update
As @Peacemoon pointed out in the comments, the navigation bar remains hidden as you navigate deeper in the navigation stack, regardless of whether or not you set
navigationBarHidden
tofalse
in subsequent views. As I said in the comments, this is either a result of poor implementation on Apple's part or just dreadful documentation (who knows, maybe there is a "correct" way to accomplish this).Whatever the case, I came up with a workaround that seems to produce the original poster's desired results. I'm hesitant to recommend it because it seems unnecessarily hacky, but without any straightforward way of hiding and unhiding the navigation bar, this is the best I could do.
This example uses three views -
View1
has a hidden navigation bar, andView2
andView3
both have visible navigation bars with titles.Setting
navigationBarHidden
tofalse
on views deeper in the navigation stack doesn't seem to properly override the preference of the view that originally setnavigationBarHidden
totrue
, so the only workaround I could come up with was using a binding to change the preference of the original view when a new view is pushed onto the navigation stack.Like I said, this is a hacky solution, but without an official solution from Apple, this is the best that I've been able to come up with.