Spaziatura e allineamento negli stack
Regolare la spaziatura e l'allineamento tra le view sovrapposte nello stack.
Spaziatura e allineamento negli stack è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Fine-Tuning Your Stacks
You can stack views in three directions, but real layouts need control over the gaps and edges between them. Let's tune that. 🎛️
The spacing Argument
Every stack accepts a spacing value that sets the gap between its children. A bigger number spreads views farther apart.
VStack(spacing: 20) {
Text("Top")
Text("Bottom")
}Zero Spacing
Pass spacing: 0 to remove the gap entirely, so children sit flush against each other. This is handy for tight, edge-to-edge designs.
Default Spacing
If you skip the argument, SwiftUI picks a sensible default gap that matches Apple's design guidelines. You only override it when you need to.
Aligning a VStack
A VStack's alignment argument controls horizontal placement. Use .leading to left-align children instead of centering them.
VStack(alignment: .leading) {
Text("Name")
Text("Email")
}Leading, Center, Trailing
VStack alignment offers .leading, .center, and .trailing. They decide whether children hug the left, the middle, or the right edge.
Aligning an HStack
An HStack's alignment is vertical instead. Use .top, .center, or .bottom to line children up along their tops, middles, or bottoms.
HStack(alignment: .top) {
Text("Tall")
Text("Short")
}Spacing and Alignment Together
You can set both arguments at once. Alignment comes first, then spacing, giving you full control over the layout in one line.
VStack(alignment: .leading, spacing: 8) {
Text("A")
Text("B")
}Meet Spacer
A Spacer is a flexible view that expands to push its neighbors apart, soaking up all the leftover space in a stack.
HStack {
Text("Left")
Spacer()
Text("Right")
}Pushing Views to Edges
Drop a Spacer between two views to send them to opposite ends, or before one view to shove it fully to the trailing side.
Baseline Alignment for Text
For mixed font sizes, HStack offers .firstTextBaseline so labels of different sizes share a clean reading line.
Quick Check
Let's check what each stack's alignment controls.
Recap: Spacing & Alignment
Well done! Use spacing to set gaps, alignment to position children, and Spacer to push views to the edges of any stack. 🚀
Domande Frequenti
La lezione «Spaziatura e allineamento negli stack» è gratuita?
Sì — il testo completo di «Spaziatura e allineamento negli stack» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.
Cosa imparerò in «Spaziatura e allineamento negli stack»?
Regolare la spaziatura e l'allineamento tra le view sovrapposte nello stack. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare SwiftUI Academy?
Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Spaziatura e allineamento negli stack»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?
Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Disporre le view in verticale
- Righe con HStack
- Sovrapporre le view con ZStack
- Spaziatura e allineamento negli stack