iosuiactivityviewcontroller

Display UIActivityViewController as form sheet on iPad


I'm trying to display a share sheet without using a popover, ie no source view, or arrow.

Here's my code, starting from a base, single view app:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func didTapShare(_ sender: UIButton) {
        let activity = UIActivityViewController(activityItems: ["banana"], applicationActivities: nil)
        
        if UIDevice.current.userInterfaceIdiom == .pad {
            activity.modalPresentationStyle = .formSheet
//            activity.preferredContentSize = CGSize(width: 400, height: 400)
        }
        
        present(activity, animated: true, completion: nil)
    }
    
}

This is of course crashing

"UIPopoverPresentationController (<UIPopoverPresentationController: 0x7fce07507900>) should have a non-nil sourceView or barButtonItem set before the presentation occurs."

But I don't want it to look like a popup, or have an arrow -- I want it to appear as a form sheet, in the middle of the screen.

Here's how it look in the Photos app:

Photos form sheet


Solution

  • Try setting the following

    activity.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)