gh-Dimillian-Skills/swiftui-ui-patterns/references/overlay.md
Thomas Ricouard 70a15d08db Add SwiftUI UI patterns skill and references
Introduces the 'swiftui-ui-patterns' skill to docs/skills.json, providing best practices and example-driven guidance for building SwiftUI views and components. Adds SKILL.md and a comprehensive set of reference files covering TabView, NavigationStack, sheets, forms, controls, grids, overlays, haptics, focus handling, media, matched transitions, split views, and more.
2026-01-04 18:26:56 +01:00

1.2 KiB
Raw Permalink Blame History

Overlay and toasts

Intent

Use overlays for transient UI (toasts, banners, loaders) without affecting layout.

Core patterns

  • Use .overlay(alignment:) to place global UI without changing the underlying layout.
  • Keep overlays lightweight and dismissible.
  • Use a dedicated ToastCenter (or similar) for global state if multiple features trigger toasts.

Example: toast overlay

struct AppRootView: View {
  @State private var toast: Toast?

  var body: some View {
    content
      .overlay(alignment: .top) {
        if let toast {
          ToastView(toast: toast)
            .transition(.move(edge: .top).combined(with: .opacity))
            .onAppear {
              DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                withAnimation { self.toast = nil }
              }
            }
        }
      }
  }
}

Design choices to keep

  • Prefer overlays for transient UI rather than embedding in layout stacks.
  • Use transitions and short auto-dismiss timers.
  • Keep the overlay aligned to a clear edge (.top or .bottom).

Pitfalls

  • Avoid overlays that block all interaction unless explicitly needed.
  • Dont stack many overlays; use a queue or replace the current toast.