History
Loading...
Loading...
September 14, 2025
.contextMenu() on any SwiftUI view to add native long-press interactions. The context menu automatically handles system styling, positioning, and accessibility - just attach it with .contextMenu { Button("Copy", action: copyAction) }Context menus should dynamically show relevant actions based on current state and permissions. Use conditional ViewBuilder syntax to show/hide menu items, update button labels based on state, and group related actions with dividers.
struct DocumentRow: View {
let document: Document
@State private var isBookmarked = false
var body: some View {
HStack {
Text(document.title)
Spacer()
if isBookmarked {
Image(systemName: "bookmark.fill")
}
}
.contextMenu {
Button("Share") {
shareDocument()
}
Button(isBookmarked ? "Remove Bookmark" : "Bookmark") {
isBookmarked.toggle()
}
if document.canEdit {
Button("Edit") {
editDocument()
}
}
Divider()
Button("Delete", role: .destructive) {
deleteDocument()
}
}
}
}Dynamic context menus prevent user confusion by only showing applicable actions, reduce cognitive load with state-aware labels, and follow platform conventions with proper action grouping and destructive button styling.