I have to admit though, I'm still a bit curious about why this doesn't seem to work for me in a two-column layout.
The Messages app on macOS is an example of a swipe action occurring without having three columns.
So, I'm still not certain about the conditions under which the .swipeAction modifier will work.
Post
Replies
Boosts
Views
Activity
I was able to work this out – the issue is that swipeActions seem to be limited to three-column navigation scenarios.
I was attempting to use .swipeAction modifiers in a layout with only two columns.
For completeness and an answer to my original question, here is a modification of the code from Alex Grebenyuk's "Triple Trouble" blog post from February 14, 2021, illustrating how to get a swipe action to work.
All I did was take Alex's code from his blog post, and add the .swipeAction modifier.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Sidebar()
Text("No Sidebar Selection")
Text("No Message Selection")
}
}
}
struct Sidebar: View {
@State private var isDefaultItemActive = true
var body: some View {
List {
Text("Favorites")
.font(.caption)
.foregroundColor(.secondary)
NavigationLink(destination: InboxView(), isActive: $isDefaultItemActive) {
Label("Inbox", systemImage: "tray.2")
}
NavigationLink(destination: SentView()) {
Label("Sent", systemImage: "paperplane")
}
}.listStyle(SidebarListStyle())
}
}
struct InboxView: View {
var body: some View {
List(Array(0...100).map(String.init), id: \.self) { message in
NavigationLink(destination: MessageDetailsView(message: message)) {
Text(message)
}
.swipeActions {
Button("Order") {
print("Awesome!")
}
.tint(.green)
}
}
.navigationTitle("Inbox")
.toolbar {
Button(action: { /* Open filters */ }) {
Image(systemName: "line.horizontal.3.decrease.circle")
}
}
}
}
struct SentView: View {
var body: some View {
Text("No Sent Messages")
.navigationTitle("Sent")
.toolbar {
Button(action: {}) {
Image(systemName: "line.horizontal.3.decrease.circle")
}
}
}
}
struct MessageDetailsView: View {
let message: String
var body: some View {
Text("Details for \(message)")
.toolbar {
Button(action: {}) {
Image(systemName: "square.and.arrow.up")
}
}
}
}