listswiftuiswiftui-navigationlinkeditmode

editMode not working in case of .sheet or .fullScreenCover but with NavigationLink


I'm trying to enable editMode, i.e to come from here:

enter image description here

to here :

enter image description here

with the following code:

struct DetailSheet: View {

@State private var items: [Item] = (0..<5).map { Item(title: "Item #\($0)") }

var body: some View {
    NavigationView { // this line to be deleted for using navigationLink
    List {
                ForEach(items) { item in
                    Text(item.title)
                }
                .onMove { (from, to) in
                    print("just a dummy")
                }
            } // List
                .navigationBarTitle("List")
              
              .navigationBarItems(leading: EditButton())
   } // this line to be deleted for using navigationLink
}

}

This works fine, if it is the first view being called from scenedelegate. If I request it from a different "first" view with

 .sheet(isPresented: $showDetailSheet, content: {DetailSheet()})

or

.fullScreenCover(isPresented: $showDetailSheet, content: {DetailSheet()})

It does not work anymore. It does work again, if I request the view with a NavigationLink ( if I delete the two lines for NavigationView{ and }.

Am I doing something wrong? If not, can anyone explain me, why editMode doesn't work, if the view was requested by .sheet?

Thanks in advance!

Edit: This is the code of the view (which is called from SceneDelegate) from where I call up DetailView

struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext

@State var showDetailSheet : Bool = false


var body: some View {
    NavigationView{
    List {
        NavigationLink(destination: DetailSheet(), label:{ Text("naviDetailSheet")})
        Button(action: {showDetailSheet = true}){Text("DetailSheet")}
        
       
     } // List
        
    .navigationBarItems(trailing: EditButton())
    } // Navi
    .sheet(isPresented: $showDetailSheet, content: {DetailSheet()})
//        .fullScreenCover(isPresented: $showDetailSheet, content: {DetailSheet()})
    
        }

}


Solution

  • One thing that can mess up EditButton (and more generally the editMode environment value) are extensions on Binding. Specifically, if you make Binding conform to Equatable, problems will occur with editMode.

    Change any reference of:

    extension Binding : Equatable where Value : Equatable {
    

    to

    extension Binding : Equatable where Value == MyType {
    

    Where MyType is the name of your type (which obviously cannot be EditMode, otherwise the problem will persist).