我在使用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")
}
}
}
这是我希望隐藏“默认”后退按钮的视图代码:
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)
}
}
}
“
通过将列表代码更新为:,我设法将其隐藏:
NavigationLink(destination: ChatView(chat: chat).navigationBarBackButtonHidden(true)) {
ChatRow(chat: chat)
}
但是下一个视图的顶部和标题之间仍然有很大的差距:
一个导航堆栈上应该只有一个NavigationView
,所以
struct ChatView: View {
...
var body: some View {
NavigationView { // << NavigationView not needed here !!!
删除标记的导航视图,应该可以。
使用Xcode 11.2,iOS 13.2进行测试