I'd like to match the behaviour of the new Freeform app by offering an Export as PDF function accessible from the title menu.
I can't figure out how to get the actual view that displays the document menu to supply as the UIActivityViewController's popoverPresentationController source view.
The Freeform app shows the PDF document sharing popover directly under the little arrow to the right of the document title, so there must be a way.
As of iOS 16, UIMenuElement
has a presentationSourceItem
property for this exact purpose. When a UIAction
or UICommand
is added to this menu, its presentationSourceItem
is the title view's arrow. You should be able to just do the following when using the default export command:
override func export(_ sender: Any?) {
let vc = /* your view controller */
if let command = sender as? UICommand {
vc.modalPresentationStyle = .popover
vc.popoverPresentationController?.sourceItem = command.presentationSourceItem
}
present(vc, animated: true)
}
or something like this if you're using a custom UIAction
:
UIAction(title: "Export as PDF", image: UIImage(systemName: "doc.text")) { action in
let vc = /* your view controller */
vc.modalPresentationStyle = .popover
vc.popoverPresentationController?.sourceItem = action.presentationSourceItem
present(vc, animated: true)
}