TechnologiesSwiftUI

PopoverAttachmentAnchor enum

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

An attachment anchor for a popover.

How it works

PopoverAttachmentAnchor describes where a popover attaches to the view that presents it. When you present a popover, SwiftUI needs an anchor point in the source view's coordinate space from which to position the bubble and draw its arrow; this enum supplies that anchor. Reach for it through the attachmentAnchor parameter of the popover(isPresented:attachmentAnchor:arrowEdge:content:) modifier whenever the default placement isn't where you want the popover to spring from.

  1. Pass an anchor to the popover modifier

    PopoverAttachmentAnchor is the type of the attachmentAnchor argument on the .popover modifier. SwiftUI uses the value you supply to compute where the popover and its arrow originate. In the example, the modifier is given attachmentAnchor: .point(.top), pinning the popover to the top of the Image that presents it.

  2. Anchor to a unit point with .point

    The .point case takes a UnitPoint and resolves to a single location within the presenting view's bounds, where (0, 0) is the top-leading corner and (1, 1) is the bottom-trailing corner. Passing .point(.top) anchors the popover to the horizontal center of the view's top edge, so the bubble appears to emerge from that spot.

  3. Anchor to the view's frame with .rect

    The other case, .rect, anchors to a rectangular region described by an Anchor<CGRect>.Source rather than a single point. Using .rect(.bounds) attaches the popover to the full frame of the presenting view, letting SwiftUI choose placement against the whole rectangle instead of the fixed location that .point(.top) specifies in the example.

  4. Pair the anchor with arrowEdge

    PopoverAttachmentAnchor decides the attachment location, while the sibling arrowEdge parameter chooses which edge of the popover the arrow points from. The example sets arrowEdge: .top alongside attachmentAnchor: .point(.top) so the arrow and the anchor agree, producing a popover that reads as hanging beneath the icon.

Try it — Change attachmentAnchor: .point(.top) to attachmentAnchor: .point(.bottom) and watch the popover and its arrow re-anchor to the bottom of the info.circle image.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

PopoverAttachmentAnchor.swift
struct PopoverAttachmentAnchorDemo: View {
    @State private var showing = true

    var body: some View {
        Image(systemName: "info.circle")
            .font(.largeTitle)
            .popover(
                isPresented: $showing,
                attachmentAnchor: .point(.top),
                arrowEdge: .top
            ) {
                Text("Anchored at .point(.top)")
                    .padding()
            }
            .padding()
    }
}
Live preview
Anchored at .point(.to…
Anchored at .point(.to…
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →