Skip to content

Commit 7ee5371

Browse files
author
Philipp Homann
authored
Merge pull request #1 from geraldcollaku/master
Define source view and source rect for the activity popover, case .pad idioms
2 parents 775af6b + 7a5f40d commit 7ee5371

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

ImageViewer/Source/GalleryViewController.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,17 +674,33 @@ open class GalleryViewController: UIPageViewController, ItemControllerDelegate {
674674

675675
case (_ as ImageViewController, let item as UIImageView):
676676
guard let image = item.image else { return }
677-
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
677+
let activityVC = activityController(with: [image])
678678
self.present(activityVC, animated: true)
679679

680680
case (_ as VideoViewController, let item as VideoView):
681681
guard let videoUrl = ((item.player?.currentItem?.asset) as? AVURLAsset)?.url else { return }
682-
let activityVC = UIActivityViewController(activityItems: [videoUrl], applicationActivities: nil)
682+
let activityVC = activityController(with: [videoUrl])
683683
self.present(activityVC, animated: true)
684684

685685
default: return
686686
}
687687
}
688+
689+
private func activityController(with items: [Any] ) -> UIActivityViewController {
690+
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
691+
692+
// for MacOS and iPad the activity view needs a source rect and view, otherwise it will crash
693+
694+
if UIDevice.current.userInterfaceIdiom == .pad { /// .pad is relevant for iPad and MacCatalyst
695+
if let popoverController = activityVC.popoverPresentationController {
696+
popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
697+
popoverController.sourceView = self.view
698+
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
699+
}
700+
}
701+
702+
return activityVC
703+
}
688704

689705
public func itemController(_ controller: ItemController, didSwipeToDismissWithDistanceToEdge distance: CGFloat) {
690706

0 commit comments

Comments
 (0)