Ale*_*dre 7 ios swift navigationview ios13 swiftui
我的修改器有问题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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我希望隐藏“默认”后退按钮的视图的代码:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
“<聊天”后退按钮仍然存在。
我通过将列表的代码更新为:
NavigationLink(destination: ChatView(chat: chat).navigationBarBackButtonHidden(true)) {
ChatRow(chat: chat)
}
Run Code Online (Sandbox Code Playgroud)
然而,顶部和下一个视图的标题之间仍然存在巨大差距:
应该只有一对一NavigationView的导航堆栈,所以
struct ChatView: View {
...
var body: some View {
NavigationView { // << NavigationView not needed here !!!
Run Code Online (Sandbox Code Playgroud)
删除标记的导航视图应该可以工作。
使用 Xcode 11.2、iOS 13.2 进行测试
| 归档时间: |
|
| 查看次数: |
3767 次 |
| 最近记录: |