Dasar-Dasar GeometryReader
Ukur ruang yang tersedia untuk menentukan ukuran konten.
Dasar-Dasar GeometryReader adalah pelajaran SwiftUI Academy gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar SwiftUI Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus SwiftUI Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.topUse 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. 🎉
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Dasar-Dasar GeometryReader” gratis?
Ya — teks lengkap “Dasar-Dasar GeometryReader” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus SwiftUI Academy, upgrade ke CoddyKit PRO. Kursus SwiftUI Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Dasar-Dasar GeometryReader”?
Ukur ruang yang tersedia untuk menentukan ukuran konten. Kamu berlatih SwiftUI Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai SwiftUI Academy?
Tidak diperlukan pengalaman sebelumnya. SwiftUI Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.
Berapa lama pelajaran “Dasar-Dasar GeometryReader” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran SwiftUI Academy ini?
Ya. Setiap pelajaran SwiftUI Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Dasar-Dasar GeometryReader
- Panduan Perataan Kustom
- Protokol Layout
- Membangun Layout Aliran