SwiftUI onDelete() without List

3.6k Views Asked by At

Is it possible to have the onDelete() modifier on ForEach work without it being within a List?

e.g. in this example how could you get the default swipe to delete behaviour?

struct ContentView: View {

    @State private var numbers = [Int]()
    @State private var currentNumber = 1

    var body: some View {
        ScrollView {
            VStack {
                Button("Add Number") {
                    self.numbers.append(self.currentNumber)
                    self.currentNumber += 1
                }
                ForEach(numbers, id: \.self) {
                    Text("\($0)")
                        .padding()
                        .background(Color.green.opacity(0.3))
                        .clipShape(RoundedRectangle(cornerRadius: 10.0))
                }
                .onDelete { offsets in
                    numbers.remove(atOffsets: offsets)
                }
            }
        }
    }
}
0

There are 0 best solutions below