Swiftui NavigationView:删除空格时保持返回按钮

发布于 2025-02-14 00:47:06 字数 896 浏览 0 评论 0原文

白空间的屏幕截图 我想在第二导航视图中删除<后按钮下方的空空间。我知道这个问题以前已经问过几次,但是我找不到这样做的好解决方案。

我尝试过

.navigationBarTitle("")
.navigationBarHidden(true)

.navigationBarTitle("", displayMode: .inline)

没有预期的结果。

有任何提示可以帮助我吗?

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
    }
}

struct FirstNavView: View {
    let listItems = ["One", "Two", "Three"]
    var body: some View {
        NavigationView {
            List(listItems, id: \.self) { item in
                NavigationLink(destination: SecondNavView(item: item)) {
                    Text(item).font(.headline)
                }
            }
        }
    }
}

Screen shot of white space
I want to remove the empty space below the <Back button in the second navigation view. I know that this question has been asked several times before, but I have not been able to find a good solution that does this.

I have tried

.navigationBarTitle("")
.navigationBarHidden(true)

and

.navigationBarTitle("", displayMode: .inline)

without the desired result.

Any hints that could help me?

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
    }
}

struct FirstNavView: View {
    let listItems = ["One", "Two", "Three"]
    var body: some View {
        NavigationView {
            List(listItems, id: \.self) { item in
                NavigationLink(destination: SecondNavView(item: item)) {
                    Text(item).font(.headline)
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や三分注定 2025-02-21 00:47:07

我认为这是对应用的修饰符的位置。

以下工作(用Xcode 13.4/ios 15.5进行测试)

“

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
        .navigationBarTitleDisplayMode(.inline)  // << here !!
    }
}

I assume it is do to place of applied modifiers.

The following works (tested with Xcode 13.4 / iOS 15.5)

demo

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
        .navigationBarTitleDisplayMode(.inline)  // << here !!
    }
}
暗恋未遂 2025-02-21 00:47:07

它像您的父视图一样没有标题,要解决此问题。

NavigationView {
    
    VStack {
        //....
    }
    
    .navigationTitle(Text("Awesome View"))
    .toolbar {
        ToolbarItem(placement: .principal){
            // Put any view (Text, Image, Stack...) you want here
        }
    }
}

It seens like your parent View hasn't a title, to solve this you need to set .navigationTitle inside NavigationView on parent View like this:

NavigationView {
    
    VStack {
        //....
    }
    
    .navigationTitle(Text("Awesome View"))
    .toolbar {
        ToolbarItem(placement: .principal){
            // Put any view (Text, Image, Stack...) you want here
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文