The newly introduced swipeActions modifier does not work directly with the List container that presents the rows of data arranged. However, it works perfectly fine in case I use a ForEach — A structure that computes views on demand from an underlying collection of identified data.
Below code doesn't work and show swipeActions
struct SwipeButtonDemoView: View {
let listItems = WWDCViewModel().sessions
var body: some View {
NavigationView {
VStack {
Spacer()
List(listItems) { session in
HStack {
Image(systemName: "play")
Text(session.title)
.font(.callout)
}
.swipeActions(edge: .leading) {
Button {
print("Bookmark")
} label: {
Label("Bookmark", systemImage: "bookmark")
}.tint(.indigo)
}
}
.listRowSeparator(.hidden)
}
.listStyle(.inset)
.navigationTitle("WWDC 21")
}
}
}
Below code works and shows swipeActions..
struct SwipeButtonDemoView: View {
let listItems = WWDCViewModel().sessions
var body: some View {
NavigationView {
VStack {
Spacer()
List {
ForEach(listItems) { session in
HStack {
Image(systemName: "play")
Text(session.title)
.font(.callout)
}
.swipeActions(edge: .leading) {
Button {
print("Bookmark")
} label: {
Label("Bookmark", systemImage: "bookmark")
}.tint(.indigo)
}
}
.listRowSeparator(.hidden)
}
}
.listStyle(.inset)
.navigationTitle("WWDC 21")
}
}
}
Why it's not working with List directly? However, it works as expected with ForEach!!!