0Pricing
SwiftUI Academy · Aula

Spacer e prioridade de layout

Afaste as visualizações com Spacer e controle o dimensionamento.

Spacer e prioridade de layout é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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.

Perguntas Frequentes

A aula “Spacer e prioridade de layout” é grátis?

Sim — o texto completo de “Spacer e prioridade de layout” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.

O que vou aprender em “Spacer e prioridade de layout”?

Afaste as visualizações com Spacer e controle o dimensionamento. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar SwiftUI Academy?

Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Spacer e prioridade de layout”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de SwiftUI Academy?

Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Adicionando preenchimento ao redor das visualizações
  2. Dimensionando visualizações com frame
  3. Planos de fundo e raio dos cantos
  4. Spacer e prioridade de layout
← Voltar para SwiftUI Academy