Skip to main content

How to show a popover view

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to show a popover view 관련

SwiftUI by Example

Back to Home

How to show a popover view | SwiftUI by Example

How to show a popover view

Updated for Xcode 15

SwiftUI has a dedicated modifier for showing popovers, which on iPadOS appear as floating balloons and on iOS slide onto the screen like a sheet.

To show a popover you need some state that determines whether the popover is currently visible, but that’s about it – unlike alerts and action sheets, popovers can contain any kind of view you want. So, just place whatever you need inside the popover and SwiftUI will take care of the rest.

For example, this shows a popover view when a button is tapped:

struct ContentView: View {
    @State private var showingPopover = false

    var body: some View {
        Button("Show Menu") {
            showingPopover = true
        }
        .popover(isPresented: $showingPopover) {
            Text("Your content here")
                .font(.headline)
                .padding()
        }
    }
}<

Download this as an Xcode projectopen in new window

A button reading “Show Menu” has been pressed. Above it is a white speech balloon containing “Your content here”.
A button reading “Show Menu” has been pressed. Above it is a white speech balloon containing “Your content here”.
Similar solutions…
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
How to show an alert | SwiftUI by Example

How to show an alert
How to show multiple alerts in a single view | SwiftUI by Example

How to show multiple alerts in a single view
How to show a menu when a button is pressed | SwiftUI by Example

How to show a menu when a button is pressed
How to show different images and other views in light or dark mode | SwiftUI by Example

How to show different images and other views in light or dark mode

이찬희 (MarkiiimarK)
Never Stop Learning.