NEW BOOK! The SwiftUI Way: A field guide to SwiftUI patterns and anti-patterns. Learn more ...NEW BOOK! The SwiftUI Way:Avoid common SwiftUI pitfalls. Learn more...
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.

The SwiftUI Way by Natalia Panferova book coverThe SwiftUI Way by Natalia Panferova book cover

Work with SwiftUI. Not against it.$35

A field guide to SwiftUI patterns and anti-patterns

The SwiftUI Wayby Natalia Panferova

  • Avoid common SwiftUI pitfalls
  • Build deeper intuition for the framework
  • Gain insights from a former SwiftUI Engineer at Apple

Work with SwiftUI. Not against it.

A field guide to SwiftUI patterns and anti-patterns

The SwiftUI Way by Natalia Panferova book coverThe SwiftUI Way by Natalia Panferova book cover

The SwiftUI Way

by Natalia Panferova

$35