GroupBox is a container view that visually groups related views together and applies a standard, styled box around its content. It is commonly used to provide a sectioned, boxed grouping for content like settings or other information. By default, it comes with a border and some padding, giving a clear visual distinction to the grouped content.
Basic Structure
The basic structure of a GroupBox in SwiftUI looks like this:
GroupBox {
// Grouped content goes here
}
Simple Example
Here’s a simple example of using GroupBox:
import SwiftUI
struct ContentView: View {
var body: some View {
GroupBox {
Text("This is inside a GroupBox")
}
}
}
In this example, the GroupBox will draw a styled box around the Text view.
GroupBox with Label
You can also provide a label for the GroupBox. This label can be any view, such as a Text, Image, or a combination of both. The label appears at the top of the box.
import SwiftUI
struct ContentView: View {
var body: some View {
GroupBox(label: Text("Settings")) {
Toggle("Enable feature", isOn: .constant(true))
Slider(value: .constant(0.5))
}
}
}
Here:
- The
GroupBoxhas a label “Settings”. - Inside the box, there is a
Toggleand aSlider.
Customizing GroupBox
You can customize the appearance of the GroupBox by applying various modifiers, such as changing the background, padding, or border style.
import SwiftUI
struct ContentView: View {
var body: some View {
GroupBox(label: Label("Settings", systemImage: "gear")) {
VStack {
Toggle("Enable feature", isOn: .constant(true))
Slider(value: .constant(0.5))
}
}
.groupBoxStyle(DefaultGroupBoxStyle()) // Using the default style
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(10)
}
}
In this example:
- The
Labelwith both text and an image is used as the label for theGroupBox. - The
GroupBoxhas additional padding, background color, and a corner radius applied to it.
Custom GroupBoxStyle
SwiftUI allows you to create a custom style for GroupBox by conforming to the GroupBoxStyle protocol. This way, you can define how the box looks and behaves.
struct CustomGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
VStack(alignment: .leading) {
configuration.label
.font(.headline)
.padding(.bottom, 5)
configuration.content
.padding()
.background(RoundedRectangle(cornerRadius: 8).fill(Color(.systemGray6)))
}
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.blue))
}
}
struct ContentView: View {
var body: some View {
GroupBox(label: Text("Custom Box")) {
Text("This is inside a custom GroupBox.")
}
.groupBoxStyle(CustomGroupBoxStyle())
}
}
In this custom style:
- The
GroupBoxcontent is inside a rounded rectangle with custom padding and background colors. - The label has its own styling separate from the content.
Example with Multiple Views
You can place multiple views inside a GroupBox to create a visually distinct section for related items.
struct ContentView: View {
var body: some View {
GroupBox(label: Label("Profile", systemImage: "person.circle")) {
VStack(alignment: .leading) {
Text("Name: John Doe")
Text("Email: [email protected]")
}
.padding()
}
.padding()
}
}
This example places a label with an image and multiple Text views inside GroupBox to create a profile section.
GroupBox is a great way to visually group related views in SwiftUI, providing an easy way to give them a boxed and labeled appearance. It works well for settings pages, forms, or other grouped content that needs visual distinction.
- Visual grouping: Provides a box around content with optional labels.
- Customizable: Can be styled with modifiers or custom styles using
GroupBoxStyle. - Use cases: Useful for settings, form sections, or any place where content needs to be visually grouped.