UIActivityViewController in Swift Crashes on iPad
The UIActivityViewController's has non-null popoverPresentationController property when running on iPad. So, try below.
if let wPPC = activityVC.popoverPresentationController {
wPPC.sourceView = some view
// or
wPPC.barButtonItem = some bar button item
}
presentViewController( activityVC, animated: true, completion: nil )
Building on @Satachito's answer: As the sourceView
you can create an (invisible) CGRect
at the place the popup should point to, and set the arrow in that direction:
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityVC.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
activityVC.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 300, height: 350)
activityVC.popoverPresentationController?.permittedArrowDirections = [.left]
}
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)