简体   繁体   English

SwiftUI NavigationView:在删除空格的同时保留后退按钮

[英]SwiftUI NavigationView: Keeping back button while removing whitespace

Screen shot of white space I want to remove the empty space below the <Back button in the second navigation view.空白区域的屏幕截图我想删除第二个导航视图中 <Back 按钮下方的空白区域。 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)
                }
            }
        }
    }
}

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中设置.navigationTitle ,如下所示:

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

I assume it is do to place of applied modifiers.我认为放置应用的修饰符是可行的。

The following works (tested with Xcode 13.4 / iOS 15.5)以下作品(使用 Xcode 13.4 / iOS 15.5 测试)

演示

struct SecondNavView: View {
    let item: String

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM