Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI navigation bar items going haywire when swipe back fails

I have a ListView, TasksView and then EditView.

The flow goes like this: you have a list cell, you tap that which takes you to TasksView When a row is tapped in TasksView it takes you to EditView.

When I half swipe back to navigate to previous view the navigation bar go bezerk and overlaps. It happens mainly if I use a navigationBarItem - (button).

In TasksView (detailView) there is a list and some navigation bar modifiers:

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

Same can be said for EditView, that when you half swipe on EditView to get back to TasksView, the same thing happens.

Here is the bug in action:

enter image description here

Anyone got any idea how to go about fixing this error?

EDIT:

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

    }
  )

I am not even using the presentationMode in the deleteList() function in order to dismiss the current view when it gets deleted. However, I am still getting the same glitch as shown in the gif above.

UPDATE:

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

}
}

The code above reproduces the bug. Where when you click "This is a test" button and then you swipe back a bit, and then go back to the last View, you will see the navigation bar going haywire!

like image 648
Osama Naeem Avatar asked May 29 '20 10:05

Osama Naeem


2 Answers

I found a simple solution to fix your problem, add this to the NavigationView

    NavigationView {
        ....
    }.navigationViewStyle(StackNavigationViewStyle())

Edit: This is the code I use to test my answer on real device and various simulators. This fixes the problem, if you find a device where this does not work let me know.

import SwiftUI

struct ContentView: 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")
            })
    }.navigationViewStyle(StackNavigationViewStyle())
}
}

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")
            }))
    }
}
} 
like image 56
workingdog support Ukraine Avatar answered Oct 13 '22 17:10

workingdog support Ukraine


I had struggled with the same issue. So I've decided to make a custom nav bar with a view modifier, and I'm using it. But I think I found another solution now. Try to add .isDetailLink(false) right after the NavigationLink.

NavigationLink(destination: DestinationView()) {
    Text("This is a test")
}
.isDetailLink(false)

like image 42
Aaron Lee Avatar answered Oct 13 '22 18:10

Aaron Lee