Menampilkan Sheet
Tampilkan tampilan modal dengan sheet dan @Binding.
Menampilkan Sheet 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.
What Is a Sheet?
A sheet is a card that slides up from the bottom to show extra content, like a form or detail, without leaving your current screen. 📄
Sheets Need a Trigger
A sheet appears when a boolean state flips to true. You store that flag in your view and flip it when the user taps something.
@State private var showSheet = falseThe .sheet Modifier
You attach .sheet to a view and bind it to your boolean. When the value is true, SwiftUI presents the content you return.
.sheet(isPresented: $showSheet) {
Text("Hello from the sheet!")
}The Dollar Sign Binding
The $ before showSheet passes a two-way binding, so SwiftUI can both read the flag and reset it to false when the sheet closes.
.sheet(isPresented: $showSheet) { DetailView() }Opening the Sheet
Set the boolean to true from a button action and the sheet animates up automatically. You never call a present function yourself. ✨
Button("Show Details") {
showSheet = true
}Putting It Together
Combine the button, the state, and the .sheet modifier on one view and you have a working modal in just a few lines.
Button("Open") { showSheet = true }
.sheet(isPresented: $showSheet) {
Text("Sheet content")
}Dismissing a Sheet
Users can swipe a sheet down to close it. To dismiss it in code, read the dismiss action from the environment inside the sheet.
@Environment(\.dismiss) private var dismissCalling dismiss()
Give the sheet a Done button that calls dismiss(). This is the polite way to let users finish and return to the main screen.
Button("Done") { dismiss() }Sheets Get Their Own State
A sheet is a normal SwiftUI view, so it can hold its own state, modifiers, and even another sheet. Treat it like any other screen.
Presenting With an Item
Use sheet(item:) when the sheet depends on a selected value. SwiftUI shows the sheet whenever that optional item is non-nil.
.sheet(item: $selectedBook) { book in
BookDetail(book: book)
}One Sheet at a Time
A view shows one sheet per modifier. For several modals, use separate flags or item bindings so they never collide on screen.
Quick Check
How do you tell SwiftUI to present a sheet?
Recap: Sheets
You learned that a sheet slides up from a boolean flag, attaches with .sheet, and closes via swipe or the dismiss action. Nice work! 🎉
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Menampilkan Sheet” gratis?
Ya — teks lengkap “Menampilkan Sheet” 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 “Menampilkan Sheet”?
Tampilkan tampilan modal dengan sheet dan @Binding. 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 “Menampilkan Sheet” 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
- Menampilkan Sheet
- Detent & Indikator Geser
- Alert dengan Tindakan
- Dialog Konfirmasi