如何删除 SwiftUI NavigationView 中的默认导航栏空间 [英] How to remove the default Navigation Bar space in SwiftUI NavigationView

查看:26
本文介绍了如何删除 SwiftUI NavigationView 中的默认导航栏空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SwiftUI 的新手(和大多数人一样),并试图弄清楚如何删除我嵌入在 NavigationView<中的 List 上方的一些 空白/代码>.

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.

在这张图片中,您可以看到 List 上方有一些空白.

In this image, you can see that there is some white space above the List.

我想要完成的是:

我试过使用:

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

其中 FileBrowserView 是一个带有 ListFileCell 定义的视图:

where FileBrowserView is a view with a List and FileCells 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.

推荐答案

出于某种原因,SwiftUI 要求您还为 .navigationBarHidden 设置 .navigationBarTitle 才能正常工作.

For some reason, SwiftUI requires that you also set .navigationBarTitle for .navigationBarHidden to work properly.

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

<小时>

更新

正如@Peacemoon 在评论中指出的那样,当您在导航堆栈中更深入地导航时,导航栏将保持隐藏状态,无论您是否将 navigationBarHidden 设置为 false在随后的观点中.正如我在评论中所说,这要么是 Apple 实施不当的结果,要么只是糟糕的文档(谁知道,也许有一种正确"的方法可以实现这一点).


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 to false 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).

无论如何,我想出了一个解决方法,似乎可以产生原始海报的预期结果.我对推荐它犹豫不决,因为它似乎不必要地hacky,但没有任何简单的方法来隐藏和取消隐藏导航栏,这是我能做的最好的.

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.

这个例子使用了三个视图 - View1 有一个隐藏的导航栏,View2View3 都有可见的带有标题的导航栏.

This example uses three views - View1 has a hidden navigation bar, and View2 and View3 both have visible navigation bars with titles.

struct View1: View {
    @State var isNavigationBarHidden: Bool = true

    var body: some View {
        NavigationView {
            ZStack {
                Color.red
                NavigationLink("View 2", destination: View2(isNavigationBarHidden: self.$isNavigationBarHidden))
            }
            .navigationBarTitle("Hidden Title")
            .navigationBarHidden(self.isNavigationBarHidden)
            .onAppear {
                self.isNavigationBarHidden = true
            }
        }
    }
}

struct View2: View {
    @Binding var isNavigationBarHidden: Bool

    var body: some View {
        ZStack {
            Color.green
            NavigationLink("View 3", destination: View3())
        }
        .navigationBarTitle("Visible Title 1")
        .onAppear {
            self.isNavigationBarHidden = false
        }
    }
}

struct View3: View {
    var body: some View {
        Color.blue
            .navigationBarTitle("Visible Title 2")
    }
}

在导航堆栈中更深的视图上将 navigationBarHidden 设置为 false 似乎没有正确覆盖最初设置 navigationBarHiddentrue,所以我能想出的唯一解决方法是在新视图被推送到导航堆栈时使用绑定来更改原始视图的首选项.

Setting navigationBarHidden to false on views deeper in the navigation stack doesn't seem to properly override the preference of the view that originally set navigationBarHidden to true, 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.

就像我说的,这是一个笨拙的解决方案,但没有来自 Apple 的官方解决方案,这是我能想到的最好的解决方案.

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.

这篇关于如何删除 SwiftUI NavigationView 中的默认导航栏空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆