Quick Tip Icon
Quick Tip

Add a Close button to SwiftUI modals on iOS 26

In iOS 26, SwiftUI introduces a new close button role designed for buttons that dismiss informational views or modals, for example, sheets showing read-only content or contextual details.

Unlike the existing cancel role, which indicates discarding edits or abandoning progress, the close role doesn't imply data loss. It doesn't perform any automatic dismissal by itself but provides internal semantics that help SwiftUI and the system understand the intent of the button, such as for accessibility and platform-consistent presentation.

Here's how we might use it in a modal sheet:

struct BirdDescriptionSheet: View {
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationStack {
            BirdDescription()
                .toolbar {
                    Button(role: .close) {
                        dismiss()
                    }
                }
        }
    }
}

This adds a system-standard close button with an xmark symbol without the need to define a custom label or icon.

iPhone showing a SwiftUI sheet with a fantail bird description and a small X close button at the top right corner of the sheet iiPhone showing a SwiftUI sheet with a fantail bird description and a small X close button at the top right corner of the sheet

It's a small but useful addition for aligning SwiftUI modals with platform conventions.

SwiftUI Fundamentals by Natalia Panferova book coverSwiftUI Fundamentals by Natalia Panferova book cover

Deepen your understanding of SwiftUI!$35

The essential guide to SwiftUI core concepts and APIs

SwiftUI Fundamentalsby Natalia Panferova

  • Explore the key APIs and design patterns that form the foundation of SwiftUI
  • Develop a deep, practical understanding of how SwiftUI works under the hood
  • Learn from a former Apple engineer who worked on widely used SwiftUI APIs

Deepen your understanding of SwiftUI!

The essential guide to SwiftUI core concepts and APIs

SwiftUI Fundamentals by Natalia Panferova book coverSwiftUI Fundamentals by Natalia Panferova book cover

SwiftUI Fundamentals

by Natalia Panferova

$35