0Pricing
SwiftUI Academy · Aula

Desativando e ativando botões

Controle a interatividade com o modificador disabled.

Desativando e ativando botões é 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.

Sometimes Tapping Should Wait

Not every button should always be tappable. A submit button might wait until a form is valid. SwiftUI handles this with the disabled modifier. 🚦

The disabled Modifier

Add disabled to any control and pass true or false. When true, the button stops responding to taps right away.

Button("Send") { send() }
    .disabled(true)

Driving It With a Condition

Instead of a fixed value, pass a condition. The button enables and disables itself as the underlying data changes.

Button("Send") { send() }
    .disabled(message.isEmpty)

Tied to State

Pair disabled with a state value and SwiftUI re-evaluates it on every change. The button reacts the moment your data updates.

@State private var name = ""

Button("Save") { save() }
    .disabled(name.isEmpty)

The Dimmed Look

A disabled button automatically appears dimmed. This visual cue tells people it is not ready yet, with no extra styling from you.

Validating a Form

A classic use is form validation: keep the action button off until every required field passes its check.

Button("Register") { register() }
    .disabled(!isFormValid)

Preventing Double Taps

During a slow task, disable the button so users cannot tap it again. Flip a loading flag while the work is running.

Button("Upload") { startUpload() }
    .disabled(isLoading)

Disabling a Whole Group

Apply disabled to a container and every control inside inherits it. One modifier can lock down an entire section at once.

VStack {
    Button("A") { a() }
    Button("B") { b() }
}
.disabled(isLocked)

Children Cannot Re-enable

Once a parent is disabled, a child cannot turn itself back on. The most restrictive disabled value in the chain wins.

Disabled Still Communicates

A disabled button is not broken; it is honest. It signals that an action is unavailable right now, guiding users toward what to do first.

Enable by Flipping the Value

To re-enable, simply make the condition false again. Update the state it depends on and SwiftUI brings the button back to life.

Button("Confirm") { confirm() }
    .disabled(!agreedToTerms)

Quick Check

One last check on enabling and disabling.

Recap

Excellent! The disabled modifier locks a button based on a condition, dims it automatically, and re-enables when the value flips back. 🚦

Perguntas Frequentes

A aula “Desativando e ativando botões” é grátis?

Sim — o texto completo de “Desativando e ativando botões” é 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 “Desativando e ativando botões”?

Controle a interatividade com o modificador disabled. 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 “Desativando e ativando botões”?

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. Criando um botão acionável
  2. Estilizando botões
  3. Executando código ao tocar
  4. Desativando e ativando botões
← Voltar para SwiftUI Academy