Swift
iOS
UIActionSheet
Swift programming
iOS development

How to present iOS UIActionSheet in Swift?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you are asking how to present an action sheet in Swift today, the practical answer is to use UIAlertController with the .actionSheet style. UIActionSheet itself is deprecated, so modern iOS code should treat it as historical background rather than the API to write new code against.

Use UIAlertController with .actionSheet

The modern replacement is straightforward:

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    @IBAction func showActions(_ sender: UIButton) {
5        let alert = UIAlertController(
6            title: "Photo",
7            message: "Choose an action",
8            preferredStyle: .actionSheet
9        )
10
11        alert.addAction(UIAlertAction(title: "Take Photo", style: .default) { _ in
12            print("take photo")
13        })
14        alert.addAction(UIAlertAction(title: "Delete", style: .destructive) { _ in
15            print("delete")
16        })
17        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
18
19        present(alert, animated: true)
20    }
21}

This is the standard answer on iPhone. It gives you the familiar action-sheet presentation and lets you define default, destructive, and cancel actions.

iPad requires popover configuration

On iPad, an action sheet is presented as a popover. If you call present without configuring the popover anchor, the app can crash at runtime.

For a button-triggered action sheet:

swift
1if let popover = alert.popoverPresentationController {
2    popover.sourceView = sender
3    popover.sourceRect = sender.bounds
4}

If the action sheet comes from a navigation bar item, set barButtonItem instead:

swift
if let popover = alert.popoverPresentationController {
    popover.barButtonItem = navigationItem.rightBarButtonItem
}

That iPad-specific requirement is the most common reason action-sheet code works on iPhone and fails elsewhere.

UIActionSheet was the old API

Older Swift and Objective-C examples may show UIActionSheet with a delegate. That is legacy code from before UIAlertController unified alerts and action sheets.

If you are maintaining very old code, you may encounter something like:

swift
// historical style, not recommended for new code
// UIActionSheet(title: ..., delegate: self, cancelButtonTitle: ..., destructiveButtonTitle: ...)

The important point is not memorizing the old syntax. It is recognizing that the current platform direction is UIAlertController.

Prefer closure-based handlers over delegate-style branching

UIAlertAction handlers keep action behavior near the action definition. That is cleaner than older delegate methods that required checking button indexes later.

This also makes the code easier to refactor because:

  • the action title and behavior stay together
  • destructive actions are explicit
  • cancel handling becomes almost trivial

In practice, modern action sheets are easier to read and less error-prone than the deprecated delegate-based approach.

Think about presentation context

Action sheets work best for a short list of mutually exclusive actions that apply to the current screen or selected item. If the list is long, deeply hierarchical, or requires explanation, a dedicated screen may be better.

That is not just UI advice. It affects how maintainable the code becomes. A clean action sheet usually has a small number of clearly named actions and minimal branching logic inside each handler.

Common Pitfalls

  • Using deprecated UIActionSheet code for new development instead of UIAlertController.
  • Forgetting to configure popoverPresentationController on iPad.
  • Putting too many actions into the sheet and making the UI hard to scan.
  • Hiding destructive behavior inside a .default action instead of marking it .destructive.
  • Trying to present the action sheet from a view controller that is not currently visible.

Summary

  • Modern Swift code should present action sheets with UIAlertController and .actionSheet.
  • Add actions with UIAlertAction and present from the current view controller.
  • On iPad, configure the popover anchor with sourceView and sourceRect or barButtonItem.
  • Treat UIActionSheet as legacy API history rather than the current solution.
  • Keep action sheets short, clear, and context-specific.

Course illustration
Course illustration

All Rights Reserved.