简体   繁体   English

SwiftUI navigationBarBackButtonHidden 没有按预期工作

[英]SwiftUI navigationBarBackButtonHidden not working as expected

I'm having an issue with the navigationBarBackButtonHidden modifier.我遇到了navigationBarBackButtonHidden修饰符的问题。 It doesn't hide the navigation back button...它不会隐藏导航后退按钮...

Here's the source code for the list:这是列表的源代码:

import SwiftUI

struct ContentView: View {
    @State var showSheet = false

    var body: some View {
        NavigationView {
            List(chatsData, id: \.self.id) { chat in
                NavigationLink(destination: ChatView(chat: chat)) {
                    ChatRow(chat: chat)
                }
            }
            .navigationBarTitle("Chats")
        }
    }
}

Here's a preview:这是一个预览: 在此处输入图片说明

Here's the code for the view I wish to hide the "default" back button:这是我希望隐藏“默认”后退按钮的视图的代码:

import SwiftUI

struct ChatView: View {
    var chat: Chat
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @State var name: String = "Some text"

    fileprivate var backButton: some View {
        Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        }, label: {
            Image(systemName: "chevron.left")
        })
    }

    var body: some View {
        NavigationView {
            VStack(alignment: .leading, spacing: 0) {
                Spacer()

                TextField("Name's placeholder", text: $name)
                    .clipShape(Rectangle())
                    .overlay(Rectangle().stroke(Color("lightgray"), lineWidth: 2))
                    .lineLimit(5)
            }
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: backButton)
            .navigationBarTitle("\(chat.id)", displayMode: .inline)
        }
    }
}

However, when clicking on a list item from the 1st screenshot, here's what I get:但是,当单击第一个屏幕截图中的列表项时,我得到的是: 在此处输入图片说明

The "< Chats" back button is still there. “<聊天”后退按钮仍然存在。

I've managed to hide it by updating the code of the List to:我设法通过将 List 的代码更新为以下内容来隐藏它:

NavigationLink(destination: ChatView(chat: chat).navigationBarBackButtonHidden(true)) {
    ChatRow(chat: chat)
}

However there's still a huge gap between the top and the title of the next view:然而,顶部和下一个视图的标题之间仍然存在巨大差距:

在此处输入图片说明

There should be only one NavigationView on one navigation stack, so一个导航堆栈上应该只有一个NavigationView ,所以

struct ChatView: View {
    ...
    var body: some View {
        NavigationView { // << NavigationView not needed here !!!

remove marked navigation view and should work.删除标记的导航视图,应该可以工作。

Tested with Xcode 11.2, iOS 13.2使用 Xcode 11.2、iOS 13.2 测试

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

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