How to group views together
About 2 min
How to group views together 관련
SwiftUI by Example
Back to Home
How to group views together | SwiftUI by Example
How to group views together
Updated for Xcode 15
If you need several views to act as one – for example, to transition together or to apply the same modifier in many places all at once – then you should use SwiftUI's Group
view.
To demonstrate this, we could create a VStack
with several pieces of text, and apply a single font()
modifier to them all at once:
Group {
Text("Line 1")
Text("Line 2")
Text("Line 3")
}
.font(.largeTitle)
You could do the same with VStack
or HStack
, of course, but by using Group
we haven't needed to specify how our text views should be arranged – if this view is placed into a larger view, that parent gets to decide whether the text views should be arranged horizontally, vertically, or some other way entirely.
Similar solutions…
How to group views together with ControlGroup | SwiftUI by Example
How to group views together with ControlGroup
How to combine text views together | SwiftUI by Example
How to combine text views together
How to blend views together | SwiftUI by Example
How to blend views together
How to group views visually using GroupBox | SwiftUI by Example
How to group views visually using GroupBox
Composing views to create a list row | SwiftUI by Example
Composing views to create a list row