0Pricing
SwiftUI Academy · Aula

Criando um botão acionável

Crie um Button com um rótulo e uma ação.

Criando um botão acionável é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 1 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.

Why Buttons Matter

Most apps come alive when you tap something. A Button is the simplest way to let people trigger an action in SwiftUI. 👆

The Button View

A Button is just a view, like Text or Image. You place it in your body and SwiftUI draws a tappable control for you.

Button("Tap me") {
    print("Tapped!")
}

Two Parts of a Button

Every button has two parts: a label that you see, and an action that runs when you tap it. Both are required.

The Simple Title Form

The quickest button takes a title string first and the action closure second. SwiftUI turns the string into a Text label for you.

Button("Save") {
    saveDocument()
}

The Action Closure

The braces hold the action closure. Whatever code you write inside runs once each time the button is tapped, and not before.

Button("Greet") {
    print("Hello there")
}

A Custom Label

Need more than text? Use the label form: pass an action, then a closure that returns any view you like for the look.

Button {
    likePost()
} label: {
    Image(systemName: "heart")
}

Labels Can Be Rich

Because the label is a view, you can combine an icon and text together inside a stack for a friendlier, clearer tappable area.

Button {
    addItem()
} label: {
    Label("Add", systemImage: "plus")
}

Buttons Live in the Body

A button goes inside your view's body, just like any other view. SwiftUI handles drawing it and listening for taps automatically.

var body: some View {
    Button("Continue") {
        goNext()
    }
}

The Tap Target

The whole label area is tappable, so a bigger label is easier to hit. Apple suggests a comfortable tap target of about 44 points.

Calling a Function on Tap

Keep the closure tidy by calling a function instead of cramming logic inline. The action just points to the work you defined elsewhere.

Button("Refresh") {
    reloadData()
}

No Tap, No Action

The action does not run when the screen loads. It waits patiently and only fires on tap, which keeps your view predictable.

Quick Check

Let's make sure the button basics clicked.

Recap

Nice work! A Button pairs a label you see with an action that runs on tap. Use the simple title form or a custom label view. ✨

Perguntas Frequentes

A aula “Criando um botão acionável” é grátis?

Sim — o texto completo de “Criando um botão acionável” é 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 “Criando um botão acionável”?

Crie um Button com um rótulo e uma ação. 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 1 de 4.

Quanto tempo leva a aula “Criando um botão acionável”?

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