SwiftUI Academy · บทเรียน

การจัดเรียง View ในแนวตั้ง

จัดกลุ่ม View จากบนลงล่างด้วย VStack

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

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

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

Why You Need Stacks

A SwiftUI body can return only one view. To show several views together, you wrap them in a container that arranges them for you. 📦

Meet VStack

A VStack stacks its child views vertically, top to bottom. It is your go-to container for column-style layouts like a title above a subtitle.

Your First VStack

Place views inside the VStack braces and they appear in order, each on its own line. The first child sits at the top.

VStack {
    Text("Hello")
    Text("World")
}

Order Matters

Children render in the exact order you write them. Swap two lines of code and the views swap places on screen. Top of the code is top of the column.

Centered by Default

By default a VStack centers its children horizontally and shrinks to fit its content, like a tidy little column hugging its views.

Mixing View Types

A VStack does not care what kind of views it holds. You can freely mix a Text, an Image, and a Button in the same vertical stack.

VStack {
    Image(systemName: "star")
    Text("Favorite")
}

How Many Views Fit?

A stack can hold up to ten direct children at once. That is plenty for most screens, and you can always nest stacks for more.

Nesting Stacks

You can put a VStack inside another VStack to build richer layouts. Nesting lets you group related views so they move together.

VStack {
    Text("Title")
    VStack {
        Text("Line 1")
        Text("Line 2")
    }
}

Stacks Are Views Too

A VStack is itself a view, so you can apply modifiers like padding or background to the whole stack at once. The group acts as one unit.

Returning a VStack from body

Since body needs a single view, returning a VStack is the classic way to show many views. The stack becomes that one returned view.

var body: some View {
    VStack {
        Text("One")
        Text("Two")
    }
}

When to Reach for VStack

Use a VStack whenever items should read top to bottom: a heading over body text, a label over a value, or a form laid out in rows.

Quick Check

Let's make sure VStack ordering is clear.

Recap: Vertical Stacking

Nice work! A VStack arranges views top to bottom in code order, centers them by default, and acts as one view you can style or nest. 🎉

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

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

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

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

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

บทเรียน “การจัดเรียง View ในแนวตั้ง” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การจัดเรียง View ในแนวตั้ง”

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

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

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

บทเรียน “การจัดเรียง View ในแนวตั้ง” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การจัดเรียง View ในแนวตั้ง
  2. แถวด้วย HStack
  3. การซ้อนชั้นด้วย ZStack
  4. ระยะห่างและการจัดแนวใน Stack
← กลับไปที่ SwiftUI Academy