SwiftUI navigationBarBackButtonHidden无法按预期工作

时间:2019-12-20 02:25:47

标签: ios swift swiftui ios13 navigationview

我遇到navigationBarBackButtonHidden修饰符的问题。它不会隐藏导航后退按钮...

以下是列表的源代码:

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

以下是预览: enter image description here

以下是我要隐藏“默认”后退按钮的视图的代码:

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

但是,当单击第一个屏幕截图中的列表项时,这是我得到的: enter image description here

“ <聊天”后退按钮仍然存在。

通过将列表代码更新为:

,我设法将其隐藏了
NavigationLink(destination: ChatView(chat: chat).navigationBarBackButtonHidden(true)) {
    ChatRow(chat: chat)
}

但是,下一个视图的顶部和标题之间仍然有很大的距离:

enter image description here

1 个答案:

答案 0 :(得分:1)

在一个导航堆栈中应该只有一个<head> <base target="_blank"> </head> ,所以

<a target="different target"> </a>

删除标记的导航视图,并且应该可以使用。

在Xcode 11.2,iOS 13.2上进行了测试