Displaying popovers
A popover is a view that can be displayed on screen to provide more information about a particular item. They include an arrow pointing to the location where they originate from. You can tap on any other screen area to dismiss the popover. Popovers are typically used on larger screens such as iPads.
In this recipe, we will create and display a popover on an iPad.
Getting ready
Create a new SwiftUI project named DisplayingPopovers
.
How to do it
Following the pattern we've used so far in this chapter, we shall first create a @State
variable whose value triggers the displaying or hiding of a popover. Then we add a .popover()
modifier that displays the popover when the @State
variable is true
. The steps are as follows:
- Just above the
body
variable in theContentView.swift
file, add astate
variable that will be used to trigger the display of the popover:@State private var showPopover = false }
- Within the
body
variable, replace theText...