0Pricing
SwiftUI Academy · บทเรียน

พื้นฐาน GeometryReader

วัดพื้นที่ว่างเพื่อกำหนดขนาดเนื้อหา

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

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

Measuring Space

Sometimes a view needs to know how big its slot is. GeometryReader hands you the exact size and position of the space it occupies. 📐

It Is a Container

A GeometryReader is a view whose body is a closure receiving a proxy that describes the available space.

GeometryReader { proxy in
    Text("Hi")
}

The Proxy

That closure parameter is a GeometryProxy. It carries the size, safe-area insets, and frame of the reader.

GeometryReader { proxy in
    let w = proxy.size.width
    Color.blue.frame(width: w)
}

Reading the Size

The most common move is proxy.size, a CGSize giving you the width and height you can fill.

GeometryReader { proxy in
    Text("\(proxy.size.width)")
}

Half the Width

Use the measured width for proportional sizing, like making a bar exactly half of the available width.

GeometryReader { proxy in
    Color.green
        .frame(width: proxy.size.width / 2)
}

It Fills by Default

A GeometryReader greedily takes all the space its parent offers, so its children align to the top-left unless you center them.

Centering Content

Because content pins top-left, wrap it or add a frame with alignment to center children inside the reader.

GeometryReader { proxy in
    Text("Center")
        .frame(width: proxy.size.width,
               height: proxy.size.height)
}

Global Coordinates

Ask for a view position in the window with proxy.frame(in: .global), handy for scroll effects and overlays.

let f = proxy.frame(in: .global)
Text("\(f.minY)")

Safe Area Insets

The proxy also exposes safeAreaInsets, so you can respect notches and home indicators in custom layouts.

let top = proxy.safeAreaInsets.top

Use It Sparingly

A GeometryReader changes layout behavior, so reach for it only when you truly need measurements, not as a default wrapper. ⚠️

Scroll-Driven Effects

Combined with a ScrollView, reading each item frame lets you build parallax and fade effects tied to scroll position.

Quick Check

Recall what the closure hands you.

Recap

You can now read available space with GeometryReader, size views proportionally, and read frames for scroll-aware effects. 🎉

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

บทเรียน “พื้นฐาน GeometryReader” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐาน GeometryReader”

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

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

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

บทเรียน “พื้นฐาน GeometryReader” ใช้เวลานานแค่ไหน

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

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

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

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

  1. พื้นฐาน GeometryReader
  2. แนวจัดตำแหน่งแบบกำหนดเอง
  3. โพรโทคอลเลย์เอาต์
  4. สร้างเลย์เอาต์แบบไหล
← กลับไปที่ SwiftUI Academy