Sovrapporre le view con ZStack
Sovrapporre le view dal fondo verso il primo piano per creare profondità.
Sovrapporre le view con ZStack è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 3 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.
Stacking in Depth
VStack goes down and HStack goes across, but sometimes you want views to overlap. That calls for a stack that works in depth. 🧱
Meet ZStack
A ZStack layers its children back to front, like sheets of glass piled on top of each other. Later views sit above earlier ones.
Your First ZStack
Inside a ZStack, the first child is the back layer and the last child is the front. Here the text sits on top of the colored background.
ZStack {
Color.blue
Text("On top")
}Back to Front Order
The drawing rule is simple: earlier children are behind, later children are in front. The last view you write wins the top spot.
Backgrounds Made Easy
A ZStack is a natural way to put a color or image behind your content. Drop a Color first, then layer your views on top of it.
Centered Overlap
By default a ZStack centers every child over the same point, so layers share one middle. You change this with an alignment argument.
ZStack(alignment: .topLeading) {
Color.gray
Text("Corner")
}Badges on Icons
A common ZStack pattern is a badge floating over an icon, like an unread count perched on the corner of a bell.
Sizing the ZStack
A ZStack takes the size of its largest child. A tiny label over a big image gives the stack the image's full footprint.
Cards with Overlays
Layer a gradient and a title over a photo to make a card. ZStack lets text stay readable on top of any background image.
ZStack {
Image("cover")
Text("Caption")
}ZStack Is a View
Like the other stacks, a ZStack is one view, so padding, frame, or background modifiers apply to the whole layered group together.
When to Reach for ZStack
Use a ZStack whenever views must overlap: a background behind content, a watermark, a badge, or text floating over an image.
Quick Check
Let's test your grasp of ZStack layering.
Recap: Layering Views
Awesome! A ZStack layers children back to front, centers them by default, and is your tool for backgrounds, badges, and overlapping cards. ✨
Domande Frequenti
La lezione «Sovrapporre le view con ZStack» è gratuita?
Sì — il testo completo di «Sovrapporre le view con ZStack» è 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 «Sovrapporre le view con ZStack»?
Sovrapporre le view dal fondo verso il primo piano per creare profondità. 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 3 di 4.
Quanto tempo richiede la lezione «Sovrapporre le view con ZStack»?
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