Show a SwiftUI Popover on iOS
Popovers aren't limited to macOS apps, see how easy it is to do on iOS.
I was recently experimenting with adding a popover inside Clean Share (app for removing tracking identifiers).
A quick search showed I needed to reach for one of these functions:
func popover<Content>(
isPresented: Binding<Bool>,
attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds),
arrowEdge: Edge = .top,
@ViewBuilder content: @escaping () -> Content
) -> some View where Content : View
func popover<Item, Content>(
item: Binding<Item?>,
attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds),
arrowEdge: Edge = .top,
@ViewBuilder content: @escaping (Item) -> Content
) -> some View where Item : Identifiable, Content : View
This was my first attempt:
struct ContentView: View {
@State private var isPresented: Bool = false
var body: some View {
Button("Show Popover") {
isPresented = true
}
.popover(isPresented: $isPresented, content: {
Text("Hello")
})
}
}
A modal isn't what I was after 😬
All is not lost though, in the iOS 16.4 SDK, we were blessed with:
func presentationCompactAdaptation(_ adaptation: PresentationAdaptation) -> some View
By specifying .popover
INSIDE the popover content we can achieve exactly what we're after.
.popover(isPresented: $isPresented, content: {
Text("Hello")
.presentationCompactAdaptation(.popover)
})
Happy popover-ing and look forward to seeing a popover or two in Clean Share in the future!