In a SwiftUI List, how do swipeActions differ from onDelete, and why does swipe to delete vanish once you add an Archive swipe action?
answer
- custom buttons per edge
- first action wins a full swipe
- roles color the destructive one
- synthesized Delete is replaced
basics
~10 sonDelete gives every row one built-in Delete; swipeActions(edge:allowsFullSwipe:content:) lets you supply your own buttons per edge. Once a row has swipe actions, SwiftUI stops synthesizing the Delete, so you add a destructive Button yourself.
solid answer
~40 s`onDelete(perform:)` on a `ForEach` gives each row a standard trailing Delete plus edit-mode delete controls. `swipeActions(edge:allowsFullSwipe:content:)` (iOS 15) is applied to the row view and takes `Button`s: `edge` defaults to `.trailing`, actions appear in order starting from that edge, and with `allowsFullSwipe` at its default of `true` a full swipe performs the first action. Apple's documentation states that once you add swipe actions, SwiftUI no longer synthesizes the Delete that `onDelete` would show, so you add `Button(role: .destructive)` yourself. Roles style buttons, `tint(_:)` recolors them, and calling the modifier twice for one edge accumulates actions. From iOS 27 a new overload with `onPresentationChanged` and `swipeActionsContainer()` bring swipe actions to stacks and grids.
code
swift · 31 linesimport SwiftUI
struct Message: Identifiable { let id = UUID(); var subject: String }
struct InboxList: View {
@State private var messages: [Message] = []
var body: some View {
List {
ForEach(messages) { message in
Text(message.subject)
.swipeActions(edge: .leading, allowsFullSwipe: false) {
Button("Pin", systemImage: "pin") { pin(message.id) }
.tint(.yellow)
}
.swipeActions(edge: .trailing) {
Button("Delete", systemImage: "trash", role: .destructive) {
messages.removeAll { $0.id == message.id }
}
Button("Archive", systemImage: "archivebox") {
messages.removeAll { $0.id == message.id }
}
.tint(.indigo)
}
}
.onDelete { messages.remove(atOffsets: $0) }
}
}
private func pin(_ id: Message.ID) {}
}go deeper
Recall that onDelete gives a standard Delete while swipeActions takes your own Buttons on the leading or trailing edge.
Explain edge order, the full-swipe default, roles versus tint, and Apple's rule that custom swipe actions replace the synthesized Delete.
Pick the full-swipe action deliberately, delete by identifier inside button actions, and gate iOS 27 container swipe actions behind availability checks.
Agree on one swipe vocabulary across the app, which edge archives and which deletes, so muscle memory carries between screens.
## Two ways to get swipe actions SwiftUI offers two mechanisms on a `List` row, and they live at different levels: | | `onDelete(perform:)` | `swipeActions(edge:allowsFullSwipe:content:)` | |---|---|---| | Attached to | the `ForEach` (`DynamicViewContent`) | the row view | | Available | iOS 13 | iOS 15 | | Actions | one standard Delete | any `Button`s you write | | Edges | trailing only | `.leading` or `.trailing` | | Data change | closure receives an `IndexSet` | each button's action runs your code | | Edit mode | adds delete controls | none documented | `onDelete` is the fast path for "remove this row". `swipeActions` is for a mail-style row with Archive, Flag, Pin and Delete. ## How swipeActions behaves The signature is `swipeActions(edge: HorizontalEdge = .trailing, allowsFullSwipe: Bool = true, content:)`. The behaviour that Apple's documentation spells out: - **Order.** Actions appear in the order you list them, starting from the swipe's originating edge. The first trailing button sits closest to the screen's trailing edge. - **Full swipe.** By default a full swipe performs the **first** action for that edge. Pass `allowsFullSwipe: false` to require a tap, which suits actions people should not fire by accident. - **Roles and tint.** A `Button(role: .destructive)` is styled red. Other buttons take a color from `tint(_:)` on the button. Symbols in labels get the fill variant automatically. - **Accumulation.** Calling the modifier more than once on the same row adds actions for that edge rather than replacing them; one call per edge is usually clearer. ## Why the Delete disappears The surprise in review is this sentence from Apple's documentation: when you add swipe actions, SwiftUI **no longer synthesizes the Delete actions** that otherwise appear when a `ForEach` uses `onDelete(perform:)`. You become responsible for creating a Delete action among your swipe actions. So a row that adds a trailing Archive button loses its automatic Delete. The fix is to write it: ```swift .swipeActions(edge: .trailing) { Button("Delete", systemImage: "trash", role: .destructive) { messages.removeAll { $0.id == message.id } } Button("Archive", systemImage: "archivebox") { archive(message.id) } .tint(.indigo) } ``` Listing Delete first puts it at the edge and makes it the full-swipe action; list Archive first if archiving should be the default gesture. Keeping `onDelete` on the `ForEach` is still useful when you also want the delete controls that edit mode shows. Because each button closure works with the **message** rather than an offset, the delete is by identifier, which stays correct even when the `ForEach` iterates a filtered or sorted array. ## Labels that change with the row Swipe action content is ordinary view-builder code, so a button can depend on the row's data. Apple's own example toggles a message between read and unread: the leading action shows a "Read" label with an open-envelope symbol when the message is unread and an "Unread" label otherwise. The closure is evaluated with the row, so the label stays in step with the model after each toggle. The same applies to conditions: an `if` inside the closure can offer Unsubscribe only for newsletters, and the actions shown on that edge change accordingly. Keep each action's work small and synchronous from the row's point of view — update the model, or hand the message to a store that does the slow part — so the list reflects the change right away. ## Beyond List: iOS 27 Before iOS 27, Apple documents swipe actions as a feature of list rows. The iOS 27 SDK adds: 1. A new overload, `swipeActions(edge:allowsFullSwipe:content:onPresentationChanged:)`, whose closure is told when the actions are shown or hidden. 2. **`swipeActionsContainer()`**, listed with it under container controls. Apple's release notes describe these as bringing custom swipe actions to views in scroll views, stacks, grids and custom layouts. An app that still deploys to iOS 17 or 18 keeps those code paths behind `if #available(iOS 27, *)`. ## Review checklist - A row with custom swipe actions has an explicit destructive Delete if deletion should be possible. - The first action on each edge is the one you want a full swipe to fire, or `allowsFullSwipe` is `false`. - Destructive work uses `role: .destructive`, and colors come from `tint(_:)`, not from row backgrounds. - Actions delete or change by identifier, not by a captured index. - Swipe actions are also reachable another way, such as a context menu or edit mode, for people who do not swipe.
- How do you make Archive, not Delete, the action a full trailing swipe performs?A full swipe performs the first action listed for that edge, so put the Archive `Button` first in the trailing `swipeActions` closure and Delete after it. If no action should fire on a full swipe, pass `allowsFullSwipe: false` instead.
- Can you add swipe actions to rows of a LazyVStack inside a ScrollView?Before iOS 27, Apple documents swipe actions as a list-row feature. The iOS 27 SDK adds `swipeActions(edge:allowsFullSwipe:content:onPresentationChanged:)` and `swipeActionsContainer()`, which Apple describes as bringing swipe actions to scroll views, stacks, grids and custom layouts. Gate that path with `if #available(iOS 27, *)` when the app deploys earlier.
- Adding swipeActions keeps the Delete that onDelete synthesizes
- A full swipe is off unless you opt in with allowsFullSwipe
- A full swipe performs the last action in the closure
- swipeActions is attached to the List rather than to each row
- Swipe actions need a tint to look destructive
Assumes the iOS 27 SDK: the List form of swipeActions dates from iOS 15, while the onPresentationChanged overload and swipeActionsContainer() for stacks and grids need iOS 27.