0
0
iOS Swiftmobile~5 mins

Swipe actions in iOS Swift

Choose your learning style9 modes available
Introduction

Swipe actions let users quickly do tasks by swiping on a list item. It makes apps easier and faster to use.

To delete an email by swiping left on it in a mail app.
To mark a message as read by swiping right on it in a chat app.
To archive a note by swiping left in a notes app.
To reveal options like edit or share on a contact by swiping on it.
Syntax
iOS Swift
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completionHandler) in
        // handle delete
        completionHandler(true)
    }
    return UISwipeActionsConfiguration(actions: [deleteAction])
}
Use trailingSwipeActionsConfigurationForRowAt for swipe actions from right to left.
Create UIContextualAction for each swipe button with style and title.
Examples
This example adds a red Delete button when swiping left on a row.
iOS Swift
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { action, view, completion in
        print("Delete tapped")
        completion(true)
    }
    return UISwipeActionsConfiguration(actions: [delete])
}
This example adds a blue Mark button when swiping right on a row.
iOS Swift
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let mark = UIContextualAction(style: .normal, title: "Mark") { action, view, completion in
        print("Mark tapped")
        completion(true)
    }
    mark.backgroundColor = .blue
    return UISwipeActionsConfiguration(actions: [mark])
}
Sample App

This app shows a list of fruits. Swipe left on any fruit to see a red Delete button. Tapping Delete prints which fruit was deleted.

iOS Swift
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let tableView = UITableView()
    var items = ["Apple", "Banana", "Cherry"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.frame = view.bounds
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let delete = UIContextualAction(style: .destructive, title: "Delete") { action, view, completion in
            print("Deleted: \(self.items[indexPath.row])")
            completion(true)
        }
        return UISwipeActionsConfiguration(actions: [delete])
    }
}
OutputSuccess
Important Notes

Swipe actions only work on UITableView or UICollectionView rows.

Always call completion(true) after handling the action to close the swipe menu.

You can add multiple actions by putting more UIContextualAction in the array.

Summary

Swipe actions let users quickly do tasks by swiping on list items.

Use UIContextualAction inside swipe configuration methods to add buttons.

Call completion handler to finish the swipe action.