TheMachineX
TheMachineX

Reputation: 209

Exiting EditMode When View Disappears

I enter the EditMode with EditButton and I want to leave it when my view disappear so the EditMode will not continue to the next view, here is my code :

@State var array = [1,2,3,4]

var body : some View {
  VStack{
    HStack{
      Spacer()
      EditButton()
    }
    List{
       ForEach(self.array){ int in
            Text(int)
       }
    }
  }
}

When the user moves to a different view with a different list, without pressing Done on the EditButton before hand, the EditMode stays, but I want it to become .inactive.

Upvotes: 1

Views: 69

Answers (1)

pawello2222
pawello2222

Reputation: 54591

You can use @Environment(\.editMode) and set it to .inactive in onDisappear:

struct ContentView: View {
    @Environment(\.editMode) var editMode
    
    var body: some View {
        VStack {
            ...
        }
        .onDisappear {
            editMode?.wrappedValue = .inactive
        }
    }
}

Upvotes: 1

Related Questions