SwiftUI Containers Referenz
Stacks, Grids, Outlines und Scroll-Erweiterungen. iOS 14 bis iOS 26.
Quellen: WWDC 2020-10031, 2022-10056, 2023-10148, 2024-10144, 2025-256
Schnellentscheidung
| Anwendungsfall | Container | iOS |
|---|---|---|
| Feste Ansichten vertikal/horizontal | VStack / HStack | 13+ |
| Überlappende Ansichten | ZStack | 13+ |
| Große scrollbare Liste | LazyVStack / LazyHStack | 14+ |
| Mehrspaltiges Grid | LazyVGrid | 14+ |
| Mehrzeiliges Grid (horizontal) | LazyHGrid | 14+ |
| Statisches Grid, präzise Ausrichtung | Grid | 16+ |
| Hierarchische Daten (Baum) | List mit children: | 14+ |
| Benutzerdefinierte Hierarchien | OutlineGroup | 14+ |
| Ein-/Ausblenden von Inhalten | DisclosureGroup | 14+ |
Teil 1: Stacks
VStack, HStack, ZStack
VStack(alignment: .leading, spacing: 12) {
Text("Titel")
Text("Untertitel")
}
HStack(alignment: .top, spacing: 8) {
Image(systemName: "star")
Text("Bewertung")
}
ZStack(alignment: .bottomTrailing) {
Image("foto")
Badge()
}
ZStack-Ausrichtungen: .center (Standard), .top, .bottom, .leading, .trailing, .topLeading, .topTrailing, .bottomLeading, .bottomTrailing
Spacer
HStack {
Text("Links")
Spacer()
Text("Rechts")
}
Spacer(minLength: 20) // Mindestgröße
LazyVStack, LazyHStack (iOS 14+)
Rendert Kinder nur, wenn sie sichtbar sind. Innerhalb von ScrollView verwenden.
ScrollView {
LazyVStack(spacing: 0) {
ForEach(items) { item in
ItemRow(item: item)
}
}
}
Angeheftete Abschnittsköpfe
ScrollView {
LazyVStack(pinnedViews: [.sectionHeaders]) {
ForEach(sections) { section in
Section(header: SectionHeader(section)) {
ForEach(section.items) { item in
ItemRow(item: item)
}
}
}
}
}
Teil 2: Grids
Grid (iOS 16+)
Nicht-lazy Grid mit präziser Ausrichtung. Lädt alle Ansichten auf einmal.
Grid(alignment: .leading, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow {
Text("Name")
TextField("Name eingeben", text: $name)
}
GridRow {
Text("E-Mail")
TextField("E-Mail eingeben", text: $email)
}
}
Modifikatoren:
gridCellColumns(_:)— Über mehrere Spalten spannengridColumnAlignment(_:)— Spaltenausrichtung überschreiben
Grid {
GridRow {
Text("Kopfzeile").gridCellColumns(2)
}
GridRow {
Text("Links")
Text("Rechts").gridColumnAlignment(.trailing)
}
}
LazyVGrid (iOS 14+)
Vertikal scrollbares Grid. Definiere Spalten; Zeilen wachsen unbegrenzt.
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
ScrollView {
LazyVGrid(columns: columns, spacing: 16) {
ForEach(items) { item in
ItemCard(item: item)
}
}
}
LazyHGrid (iOS 14+)
Horizontal scrollbares Grid. Definiere Zeilen; Spalten wachsen unbegrenzt.
let rows = [GridItem(.fixed(100)), GridItem(.fixed(100))]
ScrollView(.horizontal) {
LazyHGrid(rows: rows, spacing: 16) {
ForEach(items) { item in
ItemCard(item: item)
}
}
}
GridItem.Size
| Größe | Verhalten |
|---|---|
.fixed(CGFloat) | Exakte Breite/Höhe |
.flexible(minimum:maximum:) | Füllt Platz gleichmäßig |
.adaptive(minimum:maximum:) | Erzeugt so viele wie passen |
// Adaptiv: responsive Spaltenanzahl
let columns = [GridItem(.adaptive(minimum: 150))]
Teil 3: Outlines
List mit hierarchischen Daten (iOS 14+)
struct FileItem: Identifiable {
let id = UUID()
var name: String
var children: [FileItem]? // nil = Blatt
}
List(files, children: \.children) { file in
Label(file.name, systemImage: file.children != nil ? "folder" : "doc")
}
.listStyle(.sidebar)
OutlineGroup (iOS 14+)
Für benutzerdefinierte hierarchische Layouts außerhalb von List.
List {
ForEach(canvases) { canvas in
Section(header: Text(canvas.name)) {
OutlineGroup(canvas.graphics, children: \.children) { graphic in
GraphicRow(graphic: graphic)
}
}
}
}
DisclosureGroup (iOS 14+)
@State private var isExpanded = false
DisclosureGroup("Erweiterte Optionen", isExpanded: $isExpanded) {
Toggle("Feature aktivieren", isOn: $feature)
Slider(value: $intensity)
}
Teil 4: Häufige Muster
Foto-Grid
let columns = [GridItem(.adaptive(minimum: 100), spacing: 2)]
ScrollView {
LazyVGrid(columns: columns, spacing: 2) {
ForEach(photos) { photo in
AsyncImage(url: photo.thumbnailURL) { image in
image.resizable().aspectRatio(1, contentMode: .fill)
} placeholder: { Color.gray }
.aspectRatio(1, contentMode: .fill)
.clipped()
}
}
}
Horizontales Karussell
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 16) {
ForEach(items) { item in
CarouselCard(item: item).frame(width: 280)
}
}
.padding(.horizontal)
}
Dateibrowser
List(selection: $selection) {
OutlineGroup(rootItems, children: \.children) { item in
Label {
Text(item.name)
} icon: {
Image(systemName: item.children != nil ? "folder.fill" : "doc.fill")
}
}
}
.listStyle(.sidebar)
Teil 5: Leistung
Wann Lazy verwenden
| Größe | Scrollbar? | Verwendung |
|---|---|---|
| 1-20 | Nein | VStack/HStack |
| 1-20 | Ja | VStack/HStack in ScrollView |
| 20-100 | Ja | LazyVStack/LazyHStack |
| 100+ | Ja | LazyVStack/LazyHStack oder List |
| Grid <50 | Nein | Grid |
| Grid 50+ | Ja | LazyVGrid/LazyHGrid |
GridItem-Arrays cachen — außerhalb von body definieren:
struct ContentView: View {
let columns = [GridItem(.adaptive(minimum: 150))] // ✅
var body: some View {
LazyVGrid(columns: columns) { ... }
}
}
iOS 26 Leistung
- 6x schnellere List-Ladezeiten für 100k+ Elemente
- 16x schnellere Listenaktualisierungen
- Reduzierte verpasste Frames beim Scrollen
- Verschachtelte ScrollViews mit Lazy-Stacks laden jetzt korrekt verzögert:
ScrollView(.horizontal) {
LazyHStack {
ForEach(photoSets) { set in
ScrollView(.vertical) {
LazyVStack {
ForEach(set.photos) { PhotoView(photo: $0) }
}
}
}
}
}
Teil 6: Scroll-Erweiterungen
containerRelativeFrame (iOS 17+)
Größe von Ansichten relativ zum Scroll-Container.
ScrollView(.horizontal) {
LazyHStack {
ForEach(cards) { card in
CardView(card: card)
.containerRelativeFrame(.horizontal, count: 3, span: 1, spacing: 16)
}
}
}
scrollTargetLayout (iOS 17+)
Einrasten aktivieren.
ScrollView(.horizontal) {
LazyHStack {
ForEach(items) { ItemCard(item: $0) }
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned)
scrollPosition (iOS 17+)
Das oberste sichtbare Element verfolgen. Erfordert .id() auf jedem Element.
@State private var position: Item.ID?
ScrollView {
LazyVStack {
ForEach(items) { item in
ItemRow(item: item).id(item.id)
}
}
}
.scrollPosition(id: $position)
scrollTransition (iOS 17+)
.scrollTransition { content, phase in
content
.opacity(1 - abs(phase.value) * 0.5)
.scaleEffect(phase.isIdentity ? 1.0 : 0.75)
}
onScrollGeometryChange (iOS 18+)
.onScrollGeometryChange(for: Bool.self) { geo in
geo.contentOffset.y < geo.contentInsets.top
} action: { _, isTop in
showBackButton = !isTop
}
onScrollVisibilityChange (iOS 18+)
VideoPlayer(player: player)
.onScrollVisibilityChange(threshold: 0.2) { visible in
visible ? player.play() : player.pause()
}
Ressourcen
WWDC: 2020-10031, 2022-10056, 2023-10148, 2024-10144, 2025-256
Dokumentation: /swiftui/lazyvstack, /swiftui/lazyvgrid, /swiftui/lazyhgrid, /swiftui/grid, /swiftui/outlinegroup, /swiftui/disclosuregroup
Skills: axiom-swiftui-layout, axiom-swiftui-layout-ref, axiom-swiftui-nav, axiom-swiftui-26-ref