Alternar la visibilidad con el estado
Muestre u oculte vistas mediante un estado booleano.
Alternar la visibilidad con el estado es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 4 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.
Show and Hide on Demand
A boolean @State can decide whether a view is visible. Flip it and SwiftUI shows or hides content for you. 👀
@State private var isVisible = falseConditional Views
Inside body you can use a plain if statement. When the condition is true, SwiftUI includes the view; when false, it leaves it out.
if isVisible {
Text("Now you see me")
}A Button to Flip It
Toggle the boolean from a button action. The tiny toggle() method flips true to false and back.
Button("Toggle") {
isVisible.toggle()
}Wire It Together
Combine the button and the conditional Text in a VStack. Tapping reveals or hides the message as the state changes.
VStack {
Button("Toggle") { isVisible.toggle() }
if isVisible { Text("Hello") }
}The Full View
Here is the complete view: one boolean, one button, and one conditionally shown Text.
struct PeekView: View {
@State private var isVisible = false
var body: some View {
VStack {
Button("Toggle") { isVisible.toggle() }
if isVisible { Text("Hello") }
}
}
}Toggle the Label Text
You can also drive a label from the same boolean, so the button reads Show or Hide depending on the current state.
Button(isVisible ? "Hide" : "Show") {
isVisible.toggle()
}Hidden vs Removed
An if statement truly removes the view from the layout. Other views shift to fill the space, unlike just making something transparent.
Keep Space with opacity
If you want a view to disappear but keep its spot, change its opacity instead of removing it with an if.
Text("Hi").opacity(isVisible ? 1 : 0)Pair It with a Toggle Control
A SwiftUI Toggle control binds directly to the boolean, giving users a switch that flips your state without a custom button.
Toggle("Show details", isOn: $isVisible)One Boolean, Many Reactions
The same state value can control several views at once. Change it in one place and every dependent view reacts together.
A Smoother Reveal
Wrap the change in withAnimation and the show or hide will fade in nicely. State plus a touch of animation feels polished.
withAnimation { isVisible.toggle() }Quick Check
How do you make a Text appear only when a boolean state is true?
Recap
A boolean @State plus an if (or opacity) lets you show and hide views. Flip it with toggle and SwiftUI redraws to match. 🔀
Preguntas frecuentes
¿La lección «Alternar la visibilidad con el estado» es gratis?
Sí — el texto completo de «Alternar la visibilidad con el estado» 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 «Alternar la visibilidad con el estado»?
Muestre u oculte vistas mediante un estado booleano. 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 4 de 4.
¿Cuánto tiempo toma la lección «Alternar la visibilidad con el estado»?
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
- Por qué las vistas necesitan estado
- Declaración de propiedades @State
- Creación de un contador de pulsaciones
- Alternar la visibilidad con el estado