SwiftUI Academy · บทเรียน

การสลับการมองเห็นด้วยสถานะ

แสดงหรือซ่อน View โดยใช้สถานะบูลีน

บทเรียน 4 จาก 413 ขั้นตอน

การสลับการมองเห็นด้วยสถานะ เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “การสลับการมองเห็นด้วยสถานะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสลับการมองเห็นด้วยสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสลับการมองเห็นด้วยสถานะ”

แสดงหรือซ่อน View โดยใช้สถานะบูลีน คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสลับการมองเห็นด้วยสถานะ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใด View จึงต้องมีสถานะ
  2. การประกาศพร็อพเพอร์ตี @State
  3. การสร้างตัวนับการแตะ
  4. การสลับการมองเห็นด้วยสถานะ
← กลับไปที่ SwiftUI Academy