Enhancing Context Menus with ControlGroup in SwiftUI

KD Knowledge Diet
3 min readJun 10, 2024

--

In SwiftUI, context menus have become a versatile tool for providing context-specific actions within your app’s user interface. With the introduction of iOS 17 and iPadOS 17, SwiftUI adds the capability to use a ControlGroup inside a contextMenu(). This enhancement allows for a more compact and organized presentation of common actions, improving the overall user experience.

In this guide, we’ll explore how to leverage ControlGroup within context menus to create efficient and visually appealing user interfaces.

Introducing ControlGroup in Context Menus:

ControlGroup is a SwiftUI container that groups together multiple controls, such as buttons or toggles, into a single visual element. When used within a context menu, it allows you to present related actions in a structured and space-efficient manner.

Let’s take a look at an example of using ControlGroup within a context menu:

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")
}
}

In this code:

1. We create a context menu using `.contextMenu`.

2. Within the context menu, we use ControlGroup to group together three related actions: Cut, Copy, and Paste. Each action is represented by a Button with an associated Label and system image.

3. We include an additional Button for the Delete action, indicating its destructive nature with `role: .destructive`.

Behavior on macOS:

On macOS, the provided code results in the creation of a nested sub-menu within the context menu. This sub-menu contains the Cut, Copy, and Paste actions presented horizontally, providing a clean and organized interface.

Benefits of ControlGroup in Context Menus:

Using ControlGroup within context menus offers several advantages:

1. Compact Presentation: ControlGroup allows you to group related actions closely together, saving space and providing a more organized appearance.

2. Improved User Experience: Users can quickly access and identify common actions without unnecessary visual clutter.

3. Consistent Design: The use of ControlGroup promotes a consistent and visually pleasing design in your app’s context menus.

Conclusion:

With the introduction of ControlGroup in SwiftUI’s context menus, you can enhance the usability and aesthetics of your app’s user interface. By grouping related actions together, you provide users with a more streamlined and efficient way to interact with your app’s content. Consider using ControlGroup in your context menus to improve the overall user experience and create visually appealing interfaces. 🚀📱

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!