0Pricing
SwiftUI Academy · Aula

Ativado e desativado com Toggle

Vincule um Toggle a uma configuração booleana.

Ativado e desativado com Toggle é 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.

Meet the Toggle

A Toggle is SwiftUI's on/off switch. It is perfect for settings like notifications, dark mode, or any yes-or-no choice. 🔘

Toggle Needs a Boolean

Every Toggle reflects a Bool value. When the switch is on the value is true; when it is off the value is false. They stay in sync.

Store It in @State

Hold the on/off value in a @State property so the view can read and update it as the user flips the switch.

@State private var isOn = false

Bind with the Dollar Sign

Connect the Toggle to your state using the dollar sign. That creates a two-way binding so flips update the value automatically.

Toggle("Notifications", isOn: $isOn)

Give It a Clear Label

The first argument is the label users read next to the switch. Keep it short and describe exactly what the toggle controls.

Toggle("Dark Mode", isOn: $isDark)

React to the Value

Read the boolean anywhere in your body to react to the switch. Here the text changes based on whether the toggle is on.

Text(isOn ? "On" : "Off")

Put It in a Form

Toggles look most at home inside a Form, where they automatically gain the familiar iOS settings-row styling.

Form {
    Toggle("Wi-Fi", isOn: $wifiOn)
}

A Richer Label View

Need an icon too? Use the closure form so the label can be any view, like a Label with an SF Symbol.

Toggle(isOn: $isOn) {
    Label("Sound", systemImage: "speaker")
}

Style the Switch

Change the look with toggleStyle. The button style turns it into a tappable button instead of the default switch.

Toggle("Star", isOn: $isOn)
    .toggleStyle(.button)

Tint the Track

Set the on-color of the switch with the tint modifier, so it matches your app's accent and feels branded.

Toggle("On", isOn: $isOn)
    .tint(.green)

The Full View

Here it all comes together: one boolean, one Toggle bound to it, and a Text that responds to every flip.

struct SettingsView: View {
    @State private var isOn = false
    var body: some View {
        Toggle("Alerts", isOn: $isOn)
        Text(isOn ? "On" : "Off")
    }
}

Quick Check

How do you connect a Toggle to a boolean state value?

Recap

A Toggle binds to a Bool with the dollar sign, flips it on tap, and lets you react, style, and tint the switch. 🔁

Perguntas Frequentes

A aula “Ativado e desativado com Toggle” é grátis?

Sim — o texto completo de “Ativado e desativado com Toggle” é 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 “Ativado e desativado com Toggle”?

Vincule um Toggle a uma configuração booleana. 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 “Ativado e desativado com Toggle”?

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. Ativado e desativado com Toggle
  2. Escolhendo valores com Slider
  3. Incrementando com Stepper
  4. Selecionando opções com Picker
← Voltar para SwiftUI Academy