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

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

问题描述

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

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

我要完成的是这个

我尝试使用

.navigationBarHidden(true)

但这并没有进行任何明显的更改.

but this did not make any noticeable changes.

我目前正在这样设置我的navigationView

i'm currently setting up my navigiationView like this

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

其中FileBrowserView是具有这样定义的列表和单元格的视图

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.

推荐答案

由于某种原因,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).

无论如何,我想出了一种解决方法,该解决方法似乎可以产生原始海报的预期效果.我很犹豫地推荐它,因为它看起来似乎不必要,但如果没有任何直接的隐藏和隐藏导航栏的方法,这就是我所能做的最好的事情.

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似乎不能正确覆盖最初将navigationBarHidden设置为true的视图的首选项,因此我能想到的唯一解决方法在将新视图推送到导航堆栈时,正在使用绑定来更改原始视图的首选项.

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 NavigiationView中删除默认导航栏空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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