0Pricing
SwiftUI Academy · Lektion

Ein Flow-Layout erstellen

Erstellen Sie ein umbrechendes Layout im Tag-Stil.

Ein Flow-Layout erstellen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What Is a Flow?

A flow layout places items left to right and wraps to a new line when they run out of room, like word-wrapped text. 🏷️

Perfect for Tags

This wrapping behavior is exactly what you want for tag chips, hashtags, or filter pills of varying widths.

Start with Layout

Build it by conforming to the Layout protocol so you control wrapping yourself, since no stack wraps automatically.

struct FlowLayout: Layout {
    var spacing: CGFloat = 8
}

Measure Each Chip

In sizeThatFits, ask every subview for its size, then simulate placing them to find the total height needed.

let sizes = subviews.map {
    $0.sizeThatFits(.unspecified)
}

Track a Cursor

Keep an x and a y cursor. Move x rightward for each chip and reset when the next chip would overflow.

var x: CGFloat = 0
var y: CGFloat = 0

Detect Overflow

Before placing a chip, check if x plus its width exceeds the available width; if so, wrap to the next row.

if x + size.width > maxWidth {
    x = 0
    y += rowHeight + spacing
}

Advance the Row

After placing a chip, move x forward by its width plus spacing and grow rowHeight to the tallest chip seen.

x += size.width + spacing
rowHeight = max(rowHeight, size.height)

Place in the Second Pass

In placeSubviews, repeat the same wrapping math but call place at each computed point.

view.place(at: CGPoint(x: bounds.minX + x,
                       y: bounds.minY + y),
           proposal: .unspecified)

Return the Height

Your sizeThatFits should return the proposed width and the final y plus the last row height.

CGSize(width: maxWidth,
       height: y + rowHeight)

Drop It In

Now use it like any container: wrap your chips and the FlowLayout wraps them across as many lines as needed.

FlowLayout {
    ForEach(tags, id: \.self) { tag in
        TagChip(tag)
    }
}

Reusable Anywhere

Because it is a real Layout, your flow works with any views, adapts to width, and animates with the rest of SwiftUI. ✨

Quick Check

Recall what triggers a new row in a flow layout.

Recap

You built a wrapping FlowLayout with the Layout protocol, tracking a cursor to flow chips across rows automatically. 🎉

Häufig gestellte Fragen

Ist die Lektion „Ein Flow-Layout erstellen“ kostenlos?

Ja — der vollständige Text von „Ein Flow-Layout erstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Ein Flow-Layout erstellen“?

Erstellen Sie ein umbrechendes Layout im Tag-Stil. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Ein Flow-Layout erstellen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlagen von GeometryReader
  2. Benutzerdefinierte Alignment Guides
  3. Das Layout-Protokoll
  4. Ein Flow-Layout erstellen
← Zurück zu SwiftUI Academy