我是SwiftUI的新手(像大多数人一样),试图弄清楚如何删除我嵌入在NavigationView中的List上面的一些空白。

在此图像中,您可以看到列表上方有一些空白。

我想要完成的是:

我试过用:

.navigationBarHidden(true)

但这并没有带来任何明显的变化。

我现在像这样设置我的navigationview:

NavigationView {
    FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
        .navigationBarHidden(true)
}

其中FileBrowserView是一个带有List和FileCells的视图,定义如下:

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

我想指出的是,这里的最终目标是,您将能够单击这些单元格来更深入地导航到文件树中,因此在更深入的导航中应该在栏上显示一个后退按钮,但在我的初始视图中,我不希望顶部出现这样的任何东西。


当前回答

视图修改器使它变得简单:

//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() )
    }
}

例子:

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

其他回答

NavigationView的目的是在视图的顶部添加导航栏。在iOS中,有两种导航条:大导航条和标准导航条。

如果你不想要导航栏:

FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))

如果你想要一个大的导航栏(通常用于顶级视图):

NavigationView {
    FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
    .navigationBarTitle(Text("Title"))
}

如果你想要一个标准(内联)导航栏(通常用于子级别视图):

NavigationView {
    FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
    .navigationBarTitle(Text("Title"), displayMode: .inline)
}

希望这个答案对你有所帮助。

更多信息:苹果文档

这是一个存在于SwiftUI中的错误(在Xcode 11.2.1中仍然存在)。我写了一个ViewModifier来修复这个问题,基于现有答案的代码:

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

我为此挣扎了一段时间,但最终对我有效的是……

ZStack {
    ...
}
.edgesIgnoringSafeArea(.all) //or .edgesIgnoringSafeArea(.top)
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)

对我来说,我应用。navigationbartitle到NavigationView,而不是列表是罪魁祸首。这适用于我在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)
        }
    }
}

如果你将视图的标题设置为内联的,你想要删除空间,这并不需要在带有NavigationView的视图上完成,但也需要在导航的视图上完成。

.navigationBarTitle("", displayMode: .inline)

然后简单地改变导航栏的外观

init() {
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    UINavigationBar.appearance().shadowImage = UIImage()
}

在持有初始NavigationView的视图上。

如果您想在屏幕之间更改外观,请在适当的视图中更改外观