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

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


当前回答

类似于@graycampbell的回答,但更简单一点:

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

设置标题是必要的,因为它显示在您导航到的视图中的后退按钮旁边。

其他回答

这是一个存在于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())
    }
}

这是迄今为止我发现的最简单、最稳定的方法。你可以通过隐藏整个工具栏来隐藏导航标题和后退按钮。您可以显示,也可以选择在任何视图中显示它。你可以通过使用.toolbar(.hidden)隐藏它,并通过使用.toolbar(.visible)修饰符使它可见。

剪力/ 16 +

struct ContentView: View {
    var body: some View {
        NavigationStack {
            List {
                ForEach(0..<10) { i in
                    NavigationLink {
                        Text("Detail for Row \(i)")
                    } label: {
                        Text("Row \(i)")
                    }
                }
            }
            
            .toolbar(.hidden)
        }
    }
}

如果你的目标在iOS 16以下,你可以用NavigationView替换NavigationStack。

试着把NavigationView放在一个GeometryReader里面。

GeometryReader {
    NavigationView {
        Text("Hello World!")
    }
}

当NavigationView是根视图时,我经历过奇怪的行为。

我也尝试了本页上提到的所有解决方案,只发现@graycampbell解决方案工作良好,具有良好的动画效果。所以我试着创建一个值,我可以在整个应用程序中使用,我可以通过hackingwithswift.com的例子访问任何地方

我创建了一个ObservableObject类

class NavBarPreferences: ObservableObject {
    @Published var navBarIsHidden = true
}

把它传递给SceneDelegate中的初始视图,就像这样

var navBarPreferences = NavBarPreferences()
window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(navBarPreferences))

然后在ContentView中,我们可以像这样跟踪这个Observable对象,并创建一个指向SomeView的链接:

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

然后当访问第二个视图(SomeView)时,我们像这样再次隐藏它:

struct SomeView: View {
    @EnvironmentObject var navBarPrefs: NavBarPreferences

    var body: some View {
        Text("Hello second screen")
        .onAppear {
            self.navBarPrefs.navBarIsHidden = false
        }
    } 
}

为了保持预览工作,添加NavBarPreferences到预览,如下所示:

struct SomeView_Previews: PreviewProvider {
    static var previews: some View {
        SomeView().environmentObject(NavBarPreferences())
    }
}

对我来说,我应用。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)
        }
    }
}