Spacer y prioridad de diseño
Separe las vistas con Spacer y controle su tamaño.
Spacer y prioridad de diseño es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Meet Spacer
A Spacer is an invisible view that greedily expands to fill empty space inside a stack, pushing its neighbors apart.
Pushing Views Apart
Drop a Spacer between two views in an HStack and they fly to opposite ends, perfect for a label on the left and a value on the right.
HStack {
Text("Total")
Spacer()
Text("$42")
}Pushing to One Side
A single Spacer at the start shoves everything after it to the trailing edge. Place it first to right-align a row of content.
HStack {
Spacer()
Text("Right")
}Centering with Two Spacers
Wrap a view in two spacers and it centers itself. Each Spacer takes an equal share of the leftover space on either side.
HStack {
Spacer()
Text("Center")
Spacer()
}Spacer in a VStack
In a VStack a Spacer pushes vertically. Put one at the bottom to pin a button to the lower edge of the screen.
VStack {
Text("Top")
Spacer()
}Spacer with a Minimum Length
Give a Spacer a minLength to guarantee a gap that never collapses, even when the stack runs short on room.
HStack {
Text("A")
Spacer(minLength: 24)
Text("B")
}When Space Runs Out
When a stack is too small, SwiftUI must decide which view shrinks first. That decision is driven by each view's layout priority.
Setting Layout Priority
The layoutPriority modifier marks a view as more important. A higher number keeps its full size while lower-priority siblings shrink.
Text("Keep me full")
.layoutPriority(1)Protecting Important Text
When a long title and a subtitle compete, give the title higher layoutPriority so it shows fully and the subtitle truncates instead.
HStack {
Text("Important headline").layoutPriority(1)
Text("detail")
}Spacer vs Priority
Remember the split: Spacer distributes leftover space, while layoutPriority decides who shrinks when there is not enough to go around.
Building a Clean Row
Combine both for tidy rows: a Spacer separates label and value, and priority ensures the key text never gets clipped.
HStack {
Text("Username").layoutPriority(1)
Spacer()
Text("coddy")
}Quick Check
One last check on space and priority.
Recap
You did it! A Spacer fills and distributes space while layoutPriority controls which views shrink first, giving you balanced, robust layouts.
Preguntas frecuentes
¿La lección «Spacer y prioridad de diseño» es gratis?
Sí — el texto completo de «Spacer y prioridad de diseño» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Spacer y prioridad de diseño»?
Separe las vistas con Spacer y controle su tamaño. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar SwiftUI Academy?
No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Spacer y prioridad de diseño»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?
Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Añadir relleno alrededor de las vistas
- Dimensionamiento de vistas con frame
- Fondos y radio de esquina
- Spacer y prioridad de diseño