Ich bin neu in SwiftUI (wie die meisten Leute) und versuche herauszufinden, wie man Leerzeichen über einer Liste entfernt, die ich in eine Navigationsansicht eingebettet habe
In diesem Bild sehen Sie, dass sich über der Liste ein Leerraum befindet
Was ich erreichen möchte, ist dies
Ich habe es versucht
.navigationBarHidden(true)
Dies machte jedoch keine nennenswerten Änderungen.
Ich richte gerade meine Navigationsansicht so ein
NavigationView {
FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
.navigationBarHidden(true)
}
Dabei ist FileBrowserView eine Ansicht mit einer Liste und Zellen, die wie folgt definiert sind
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.
quelle
navigationBarHidden
tofalse
on the next view should unhide the navigation bar, but it doesn't. I ultimately got fed up with just how poorly documented SwiftUI was and went back to UIKit, and the fact that at least 20 people came here just to learn how to hide the navigation bar speaks pretty poorly for Apple's implementation and/or documentation. Sorry I don't have a better answer for you.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:
FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
If you want a large navigation bar (generally used for your top-level views):
NavigationView { FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL)) .navigationBarTitle(Text("Title")) }
If you want a standard (inline) navigation bar (generally used for sub-level views):
NavigationView { FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL)) .navigationBarTitle(Text("Title"), displayMode: .inline) }
Hope this answer will help you.
More information: Apple Documentation
quelle
NavigationView
. The purpose of aNavigationView
is not solely to display a navigation bar.View Modifiers made it easy:
//ViewModifiers.swift struct HiddenNavigationBar: ViewModifier { func body(content: Content) -> some View { content .navigationBarTitle("", displayMode: .inline) .navigationBarHidden(true) } } extension View { func hiddenNavigationBarStyle() -> some View { modifier( HiddenNavigationBar() ) } }
Example:
import SwiftUI struct MyView: View { var body: some View { NavigationView { VStack { Spacer() HStack { Spacer() Text("Hello World!") Spacer() } Spacer() } .padding() .background(Color.green) //remove the default Navigation Bar space: .hiddenNavigationBarStyle() } } }
quelle
I also tried all the solutions mentioned on this page and only found @graycampbell solution the one to be working well, with well-working animations. So I tried to create a value I can just use throughout the app that I can access anywhere by the example of hackingwithswift.com
I created an
ObservableObject
classclass NavBarPreferences: ObservableObject { @Published var navBarIsHidden = true }
And pass it on to the initial view in the
SceneDelegate
like sovar navBarPreferences = NavBarPreferences() window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(navBarPreferences))
Then in the
ContentView
we can keep track of this Observable object like so and create a link toSomeView
:struct ContentView: View { //This variable listens to the ObservableObject class @EnvironmentObject var navBarPrefs: NavBarPreferences var body: some View { NavigationView { NavigationLink ( destination: SomeView()) { VStack{ Text("Hello first screen") .multilineTextAlignment(.center) .accentColor(.black) } } .navigationBarTitle(Text(""),displayMode: .inline) .navigationBarHidden(navBarPrefs.navBarIsHidden) .onAppear{ self.navBarPrefs.navBarIsHidden = true } } } }
And then when accessing the second view (SomeView), we hide it again like this:
struct SomeView: View { @EnvironmentObject var navBarPrefs: NavBarPreferences var body: some View { Text("Hello second screen") .onAppear { self.navBarPrefs.navBarIsHidden = false } } }
To keep previews working add the NavBarPreferences to the preview like so:
struct SomeView_Previews: PreviewProvider { static var previews: some View { SomeView().environmentObject(NavBarPreferences()) } }
quelle
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:public struct NavigationBarHider: ViewModifier { @State var isHidden: Bool = false public func body(content: Content) -> some View { content .navigationBarTitle("") .navigationBarHidden(isHidden) .onAppear { self.isHidden = true } } } extension View { public func hideNavigationBar() -> some View { modifier(NavigationBarHider()) } }
quelle
If you set the title as inline for the View you want remove the space on, this doesn't need to be done on a view with a NavigationView, but the one navigated too.
.navigationBarTitle("", displayMode: .inline)
then simply change the Navigation bars appearance
init() { UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default) UINavigationBar.appearance().shadowImage = UIImage() }
on the view that holds the initial NavigationView.
If you want to change the Appearance from screen to screen change the appearance in the appropriate views
quelle
You could extend native View protocol like this:
extension View { func hideNavigationBar() -> some View { self .navigationBarTitle("", displayMode: .inline) .navigationBarHidden(true) } }
Then just call e.g.:
ZStack { *YOUR CONTENT* } .hideNavigationBar()
quelle
For me, I was applying the
.navigationBarTitle
to theNavigationView
and not toList
was the culprit. This works for me on Xcode 11.2.1:struct ContentView: View { var body: some View { NavigationView { List { NavigationLink(destination: DetailView()) { Text("I'm a cell") } }.navigationBarTitle("Title", displayMode: .inline) } } }
quelle
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.
quelle
For me it was because I was pushing my NavigationView from an existing. In effect having one inside the other. If you are coming from a NavigationView you do not need to create one inside the next as you already inside a NavigatonView.
quelle
Really loved the idea given by @Vatsal Manot To create a modifier for this.
Removing
isHidden
property from his answer, as I don't find it useful as modifier name itself suggests that hide navigation bar.// Hide navigation bar. public struct NavigationBarHider: ViewModifier { public func body(content: Content) -> some View { content .navigationBarTitle("") .navigationBarHidden(true) } } extension View { public func hideNavigationBar() -> some View { modifier(NavigationBarHider()) } }
quelle
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") } } } }
quelle
My solution for this problem was the same as suggested by @Genki and @Frankenstein.
I applied two modifiers to the inner list (NOT the NavigationView) to get rid of the spacing:
.navigationBarTitle("", displayMode: .automatic) .navigationBarHidden(true)
On the outer NavigationView, then applied
.navigationBarTitle("TITLE")
to set the title.quelle
SwiftUI 2
There is a dedicated modifier to make the navigation bar take less space:
There's no longer need to hide the navigation bar or set its title.
quelle
Try putting the
NavigationView
inside aGeometryReader
.GeometryReader { NavigationView { Text("Hello World!") } }
I’ve experienced weird behavior when the
NavigationView
was the root view.quelle