0Pricing
SwiftUI Academy · Lesson

Toggling Visibility with State

Show or hide views using a boolean state.

Toggling Visibility with State is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 = false

Conditional 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. 🔀

Frequently asked questions

Is the “Toggling Visibility with State” lesson free?

Yes — the full text of “Toggling Visibility with State” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Toggling Visibility with State”?

Show or hide views using a boolean state. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Toggling Visibility with State” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Views Need State
  2. Declaring @State Properties
  3. Building a Tap Counter
  4. Toggling Visibility with State
← Back to SwiftUI Academy