Activación y desactivación con Toggle
Vincule un Toggle a una configuración booleana.
Activación y desactivación con Toggle es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 1 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 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 = falseBind 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. 🔁
Preguntas frecuentes
¿La lección «Activación y desactivación con Toggle» es gratis?
Sí — el texto completo de «Activación y desactivación con Toggle» 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 «Activación y desactivación con Toggle»?
Vincule un Toggle a una configuración booleana. 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 1 de 4.
¿Cuánto tiempo toma la lección «Activación y desactivación con Toggle»?
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
- Activación y desactivación con Toggle
- Elección de valores con Slider
- Incremento con Stepper
- Selección de opciones con Picker