DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

Exploring ControlGroup in SwiftUI

In SwiftUI, a ControlGroup provides a way to visually group controls together, making your UI more intuitive and structured. It helps you present related controls in a unified manner, enhancing the user experience by logically grouping similar actions.

Syntax Overview

Here's a basic example of how to use a ControlGroup:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            ControlGroup {
                Button("Action 1") {
                    // Action 1 code
                }
                Button("Action 2") {
                    // Action 2 code
                }
            }
            .controlGroupStyle(.automatic)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

ControlGroup Automatic

In this example, we create a ControlGroup inside a VStack, containing two buttons. The .controlGroupStyle(.automatic) modifier ensures that the group adopts the appropriate style based on the context it's used in.

Customizing ControlGroup

While the automatic style is convenient, you can customize ControlGroup to match your app's design. SwiftUI offers different styles, such as navigation and compact.

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            ControlGroup {
                Button("Action 1") {
                    // Action 1 code
                }
                Button("Action 2") {
                    // Action 2 code
                }
            }
            .controlGroupStyle(.navigation)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

ControlGroup Navigation

In the example above, we use .controlGroupStyle(.navigation) to give the control group a navigation-based appearance, suitable for toolbar items.

Practical Example

Consider an app where users frequently perform text formatting actions. You can use a ControlGroup to group formatting buttons together, providing a clean and organized interface:

import SwiftUI

struct FormattingView: View {
    var body: some View {
        VStack {
            Text("Formatted Text")
                .font(.title)

            ControlGroup {
                Button(action: boldText) {
                    Image(systemName: "bold")
                }
                Button(action: italicizeText) {
                    Image(systemName: "italic")
                }
                Button(action: underlineText) {
                    Image(systemName: "underline")
                }
            }
            .controlGroupStyle(.menu)
        }
    }

    func boldText() {
        // Bold text code
    }

    func italicizeText() {
        // Italicize text code
    }

    func underlineText() {
        // Underline text code
    }
}
Enter fullscreen mode Exit fullscreen mode

.menu:

ControlGroup menu

.compactMenu

ControlGroup compactMenu

In this FormattingView, we group three formatting actions—bold, italic, and underline—using ControlGroup with a .menu / .compactMenu style, making the actions easily accessible and visually appealing.

Interesing Use Cases

Control groups work great on (context) menu's, see the following example:

import SwiftUI

struct ContextMenuView: View {
    var body: some View {
        VStack {
            Text("Right-click (or long press) to show context menu")
                .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")
                        }
                    }
                }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

ControlGroup in contextmenu

Conclusion

ControlGroup is a powerful feature in SwiftUI that enhances the organization of controls in your app. By logically grouping related actions, you can improve the user experience and create a more intuitive interface. Experiment with different control group styles to find the best fit for your app's design.

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

AWS Security LIVE! Stream

Stream AWS Security LIVE!

See how AWS is redefining security by design with simple, seamless solutions on Security LIVE!

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay