I tried the example from https://developer.apple.com/documentation/swiftui/editmode. It's not working for me.
struct ContentView: View {
@Environment(\.editMode)
private var editMode
@State
private var name = "Maria Ruiz"
var body: some View {
NavigationView {
Form {
Text(String(editMode!.wrappedValue.isEditing))
if editMode?.wrappedValue.isEditing == true {
TextField("Name", text: $name)
} else {
Text("test")
}
}
.animation(nil, value: editMode?.wrappedValue)
.toolbar { // Assumes embedding this view in a NavigationView.
EditButton()
}
}
}
}
It shows the texts "false" and "test", before and after clicking Edit. What am I missing? I'm using XCode 14.0.1 and the deployment target is iOS 16. I also tried on a real iPhone and on iOS 15.5 in the Simulator. Thanks for any help.
I've found that editMode
works very strangely and only some of the time when you know how it works.
Try extracting the parts that access the editMode
property from the container that changes based on it, like List
/Form
.
// container that does editing
// changes based on editMode
Form {
EditingView() // extract to new view
}
// EditingView
@Environment(\.editMode) private var editMode
if editMode?.wrappedValue.isEditing == true {
Text("Editing")
} else {
Text("Not Editing")
}