NEW BOOK! Swift Gems: 100+ tips to take your Swift code to the next level. Learn more ...NEW BOOK! Swift Gems:100+ advanced Swift tips. Learn more...
Quick Tip Icon
Quick Tip

ControlGroup in context menus in SwiftUI

Starting from iOS 17 and iPadOS 17 we can now use a ControlGroup inside a contextMenu(), enabling more compact access to common actions.

Screenshot of a context menu open showing cut, copy and past all in a horizontal stack.
ContentView()
    .contextMenu {
        ControlGroup {
            Button {
                // cut action
            } label: {
                Label("Cut", systemImage: "scissors")
            }
            
            Button {
                // copy action
            } label: {
                Label("Copy", systemImage: "doc.on.doc")
            }
            
            Button {
                // paste action
            } label: {
                Label("Paste", systemImage: "doc.on.clipboard")
            }
        }
        
        Button(role: .destructive) {
            // delete action
        } label: {
            Label("Delete", systemImage: "trash")
        }
    }

On macOS this code creates a nested sub-menu with cut, copy and paste as items.

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Check out our new book!

Swift Gems

100+ tips to take your Swift code to the next level

Swift Gems

100+ tips to take your Swift code to the next level

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms
  • Practical Swift insights from years of development, applicable from iOS to server-side Swift