I made a new project to figure out how List and editing List works and there was no problem at all, everything was going smoothly.
Then I transferred the code over into an existing project but I ran into an unusual problem: when I try to use custom button to change the EditMode and make List editable it does not work. However when I use the original EditButton the List goes into editing mode.
Do you have any ideas, why is this happening?
This is the code:
The thing is the editMode environment variable changes with every click because the text in the custom button changes accordingly but the List does not go into editing mode. Strange.
Then I transferred the code over into an existing project but I ran into an unusual problem: when I try to use custom button to change the EditMode and make List editable it does not work. However when I use the original EditButton the List goes into editing mode.
Do you have any ideas, why is this happening?
This is the code:
Code Block swift @Environment(\.editMode) var editMode var body: some View { VStack { HStack { Spacer() EditButton() .accentColor(.black) Button(action: { editMode?.wrappedValue = editMode?.wrappedValue == .active ? .inactive : .active }) { Text(editMode?.wrappedValue == .active ? "Saved" : "Modify") } } .padding(.horizontal) if emptyDictionary { Divider() Spacer() Text("Nothing here yet") .font(.light18) Spacer() } else { List { ForEach(keys, id:\.self) { color in if !favourites(color: color).isEmpty { Section(header: headerView(color: color)) { ForEach(favourites(color: color)) { fav in versRow(vers: fav, color: color) } .onDelete(perform: {indexSet in store.deleteFavourites(color: color, indexSet: indexSet) }) .onMove { (indexSet, newOffset) in store.moveFavourites(color: color, indexSet: indexSet, newOffset: newOffset) } } } } } .padding(.horizontal) } } }
The thing is the editMode environment variable changes with every click because the text in the custom button changes accordingly but the List does not go into editing mode. Strange.
Thanks for clarifying. And I can confirm that the List does not go into edit mode using the custom button.Not a NavigationView but a TabView.
In a simplified project like mine, the same workaround found for NavigationView works.
Code Block @State var editMode: EditMode = .inactive //<- Declare the @State var for editMode var body: some View { TabView { Text("Dummy") .tabItem {Text("1")} //... FavoritesView(dict: favoritesDict) .tabItem {Text("2")} .environment(\.editMode, $editMode) //<- And pass the binding explicitly //... } }
Please try with your project.