Sichtbarkeit mit State umschalten
Blenden Sie Views mithilfe eines booleschen States ein oder aus.
Sichtbarkeit mit State umschalten ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🔀
Häufig gestellte Fragen
Ist die Lektion „Sichtbarkeit mit State umschalten“ kostenlos?
Ja — der vollständige Text von „Sichtbarkeit mit State umschalten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Sichtbarkeit mit State umschalten“?
Blenden Sie Views mithilfe eines booleschen States ein oder aus. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Sichtbarkeit mit State umschalten“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Warum Views State benötigen
- @State-Eigenschaften deklarieren
- Einen Tippzähler erstellen
- Sichtbarkeit mit State umschalten