Alternando a visibilidade com estado
Mostre ou oculte visualizações usando um estado booleano.
Alternando a visibilidade com estado é 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.
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. 🔀
Perguntas Frequentes
A aula “Alternando a visibilidade com estado” é grátis?
Sim — o texto completo de “Alternando a visibilidade com estado” é 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 “Alternando a visibilidade com estado”?
Mostre ou oculte visualizações usando um estado booleano. 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 “Alternando a visibilidade com estado”?
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
- Por que as visualizações precisam de estado
- Declarando propriedades @State
- Criando um contador de toques
- Alternando a visibilidade com estado