0Pricing
SwiftUI Academy · Lezione

Presentare una Sheet

Mostri una vista modale con una sheet e un binding.

Presentare una Sheet è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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 = false

The .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 dismiss

Calling 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! 🎉

Domande Frequenti

La lezione «Presentare una Sheet» è gratuita?

Sì — il testo completo di «Presentare una Sheet» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.

Cosa imparerò in «Presentare una Sheet»?

Mostri una vista modale con una sheet e un binding. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare SwiftUI Academy?

Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Presentare una Sheet»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?

Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Presentare una Sheet
  2. Detents e indicatori di trascinamento
  3. Alert con azioni
  4. Finestre di dialogo di conferma
← Torna a SwiftUI Academy