我有一个 ListView
, TasksView
和 EditView
。
流程如下:您有一个列表单元格,您点击单元格会进入TasksView
。在TasksView
中,如果单击行,则会进入EditView
。
当我向后滑动一半以导航到先前的视图时,导航栏会变得混乱并重叠。如果使用navigationBarItem-(button),则主要会出现此问题。
在TasksView(detailView)
中有一个列表和一些导航栏修改器:
ZStack {
List {
// code here
}
}
.onAppear { UITableView.appearance().separatorStyle = .none }
.onDisappear { UITableView.appearance().separatorStyle = .none }
.background(Color("primaryBackground"))
.edgesIgnoringSafeArea(.bottom)
.navigationBarTitle("\(listItem.name ?? "")", displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {self.deleteList()}) {
Image(systemName: "trash.circle.fill")
}
)
EditView
也是一样的情况,当你在EditView
上向一半滑动以返回到TasksView
时,相同的问题会发生。以下是该错误的演示:
![enter image description here](https://istack.dev59.com/j7JtX.gif)
编辑:
struct TasksView: View {
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var taskControl: TaskControl
@EnvironmentObject var addNewTaskData: AddNewTaskViewDataFlow
@ObservedObject var dataPickerData : DatePickerDataFlowV2
@FetchRequest(entity: ONList.entity(), sortDescriptors: []) var listsDataSource: FetchedResults<ONList>
@Environment(\.managedObjectContext) var listMOC
var listItem: ONList
var keyboardPublisher: AnyCancellable
// defining the presentationMode here
.......
ZStack {
NavigationLink(destination: ListOptions(listItem: listItem), tag: 1, selection: self.$navigationSelectionTag) {
EmptyView()
}
Color("primaryBackground")
.edgesIgnoringSafeArea(.top)
//... more code here
}
.onAppear {
UITableView.appearance().separatorStyle = .none
self.taskControl.taskViewerSeperator = true
}
.onDisappear {
UITableView.appearance().separatorStyle = .none
print("Bye Task View")
if UIDevice.current.userInterfaceIdiom == .phone {
self.taskControl.taskViewerSeperator = false
}
self.keyboardPublisher.cancel()
}
.navigationBarTitle("\(listItem.name ?? "")", displayMode: .inline)
.background(Color("primaryBackground"))
.edgesIgnoringSafeArea(.bottom)
.navigationBarItems(trailing:
HStack {
Button(action: {
self.navigationSelectionTag = 1
}, label: {
Image(systemName: "gear")
})
Button(action: {
self.deleteList()
}) {
Image(systemName: "trash.circle.fill")
}.padding()
}
)
我甚至没有在deleteList()
函数中使用presentationMode来在删除当前视图时关闭它。然而,我仍然遇到了与上面动画展示的相同的故障。
更新:
struct TestingCoreData: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DestinationView()) {
Text("This is a test")
}
}.navigationBarTitle(Text("Master"), displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
print("tapped")
}) {
Text("Button")
}
)
}
}
}
struct DestinationView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
List {
Text("DestinationView")
.padding(.top, 100)
.navigationBarTitle(Text("Destination"), displayMode: .inline)
.navigationBarItems(trailing: Button(action: {
self.presentationMode.wrappedValue.dismiss()
}, label: {
Text("second")
}))
}
}
}
以上代码重现了该错误。当您单击“这是一个测试”按钮,然后稍微向后滑动,然后返回到上一个视图时,您将看到导航栏变得混乱不堪!