Group

A type that collects multiple instances of a content type --- like views, scenes, or commands --- into a single unit.

ComponentiOS 13.0+macOS 10.15+tvOS 13.0+watchOS 6.0+Group in SwiftUI

Declaration

React
import { Group } from 'swiftui-react-native';
Swift
@frozen struct Group<Content>

Initializers

2 SwiftUI initializers back <Group>. The props you pass pick one.

React
<Group>
  children?: ReactNode
</Group>
Swift
@export(implementation) nonisolated init(
    @ContentBuilder content: () -> Content
)
  • childrenReactNode

    Content. Strings become Text; React Native views are laid out by Yoga and placed by SwiftUI.

    Swift content: () -> Content

Also takes the common view props: modifiers, style and testID.

Overview

Use a group to collect multiple views into a single instance, without affecting the layout of those views, like an SwiftUI.HStack, SwiftUI.VStack, or SwiftUI.Section would. After creating a group, any modifier you apply to the group affects all of that group's members. For example, the following code applies the SwiftUI.Font.headline font to three views in a group.

Group {
    Text("SwiftUI")
    Text("Combine")
    Text("Swift System")
}
.font(.headline)

Because you create a group of views with a SwiftUI.ContentBuilder, you can use the group's initializer to produce different kinds of views from a conditional, and then optionally apply modifiers to them. The following example uses a Group to add a navigation bar title, regardless of the type of view the conditional produces:

Group {
    if isLoggedIn {
        WelcomeView()
    } else {
        LoginView()
    }
}
.navigationBarTitle("Start")

The modifier applies to all members of the group --- and not to the group itself. For example, if you apply View.onAppear(perform:) to the above group, it applies to all of the views produced by the if isLoggedIn conditional, and it executes every time isLoggedIn changes.

Because a group of views itself is a view, you can compose a group within other content builders, including nesting within other groups. This allows you to add large numbers of views to different content builder containers. The following example uses a Group to collect 10 SwiftUI.Text instances, meaning that the vertical stack's content builder returns only two views --- the group, plus an additional SwiftUI.Text:

var body: some View {
    VStack {
        Group {
            Text("1")
            Text("2")
            Text("3")
            Text("4")
            Text("5")
            Text("6")
            Text("7")
            Text("8")
            Text("9")
            Text("10")
        }
        Text("11")
    }
}

You can initialize groups with several types other than SwiftUI.View, such as SwiftUI.Scene and SwiftUI.ToolbarContent. The closure you provide to the group initializer uses the corresponding builder type (SwiftUI.SceneBuilder, SwiftUI.ToolbarContentBuilder, and so on), and the capabilities of these builders vary between types. For example, you can use groups to return large numbers of scenes or toolbar content instances, but not to return different scenes or toolbar content based on conditionals.

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • AccessibilityRotorContent
  • Commands
  • CustomizableToolbarContent
  • Scene
  • SceneAccessoryContent
  • Swift.Copyable
  • Swift.Escapable
  • TabContent
  • TableColumnContent
  • TableRowContent
  • ToolbarContent
  • View

See also

More in Views.