SwiftUI Academy · Lektion

Innenabstand um Views hinzufügen

Verwenden Sie padding, um großzügige Abstände zu schaffen.

Lektion 1 von 413 Schritte

Innenabstand um Views hinzufügen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.

Why Views Feel Cramped

By default a SwiftUI view sits flush against its neighbors and the screen edge. Padding is the breathing room you add so content never feels squeezed.

Meet the padding Modifier

The padding modifier adds space on the outside of a view. Attach it like any other modifier and SwiftUI grows the view's footprint to make room.

Text("Hello")
    .padding()

The Default Amount

Calling padding() with no value uses a system default that adapts to the platform, usually around 16 points. It is a safe, consistent starting point.

Setting an Exact Amount

Pass a number to control the gap precisely. Here padding(40) adds 40 points of space on every side of the text.

Text("Spaced")
    .padding(40)

Padding One Edge

You can target a single edge instead of all four. This adds 30 points only on top, leaving the other sides untouched.

Text("Top gap")
    .padding(.top, 30)

Horizontal and Vertical

The .horizontal and .vertical edge sets are handy shortcuts. This pads the left and right sides at once with one clean call.

Text("Sides")
    .padding(.horizontal, 24)

Combining Edge Sets

Stack two padding calls to mix amounts per direction. Here the view gets wide horizontal margins and a smaller vertical gap.

Text("Mixed")
    .padding(.horizontal, 32)
    .padding(.vertical, 8)

Order Matters

Modifiers apply outward in order, so where you place padding changes the result. Padding before a background pushes the color outward with the content.

Text("Hi")
    .padding()
    .background(.yellow)

Padding Then Background

Swap the order and the difference is clear: a background applied first, then padding, leaves the color tight to the text with empty space around it.

Text("Hi")
    .background(.yellow)
    .padding()

Padding on Containers

Padding works on stacks and any view, not just text. Wrap a VStack and pad it to push the whole group inward from the screen edges.

VStack {
    Text("Line one")
    Text("Line two")
}
.padding()

Padding vs Margins

SwiftUI has no separate margin concept. Padding is your single tool for spacing, and chaining it gives you full control over every edge.

Quick Check

Test your grip on the padding modifier.

Recap

Nice work! You learned that padding adds breathing room outside a view, with defaults, exact amounts, specific edges, and an order that shapes backgrounds.

Kostenlos starten

Lerne Swift mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
30
Lektionen
120

Häufig gestellte Fragen

Ist die Lektion „Innenabstand um Views hinzufügen“ kostenlos?

Ja — der vollständige Text von „Innenabstand um Views hinzufügen“ 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 „Innenabstand um Views hinzufügen“?

Verwenden Sie padding, um großzügige Abstände zu schaffen. 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 1 von 4.

Wie lange dauert die Lektion „Innenabstand um Views hinzufügen“?

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. Innenabstand um Views hinzufügen
  2. Views mit frame dimensionieren
  3. Hintergründe und Eckradius
  4. Spacer und Layout-Priorität
← Zurück zu SwiftUI Academy