Why does editMode in SwiftUI not work in toolbar?

284 Views Asked by At

I'm learning the editMode in SwiftUI. I found that if I put the EditButton in a stack, the editMode will switch between .active and .inactive. If I put the EditButton in the toolbar, the editMode does not change if I tap the EditButton.

The following works.

struct FirstView: View {
    
    @Environment(\.editMode) private var editMode

    var body: some View {
        VStack {
            Text(editMode?.wrappedValue == .active ? "Active" : "Inactive")
            EditButton()
        }
    }
}

The following does not work.

struct FirstView: View {
    
    @Environment(\.editMode) private var editMode

    var body: some View {
        NavigationView {
            Text(editMode?.wrappedValue == .active ? "Active" : "Inactive")
                .toolbar {
                    EditButton()
                }
        }
    }
}

The second question is why I cannot use the isEditing property as below?

Text((editMode?.isEditing ?? false) ? "Active" : "Inactive")

// The following works:
Text(editMode?.wrappedValue.isEditing ?? false ? "Active" : "Inactive")

Does anyone know why? Thanks!

0

There are 0 best solutions below