iosswiftuiactivityviewcontroller

Dismiss Activity View Controller above view


How can I dismiss an UIActivityViewController by tapping above the view.

// Setting url
let secondActivityItem : NSURL = NSURL(string: url)!

let activityViewController : UIActivityViewController = UIActivityViewController(
    activityItems: [secondActivityItem], applicationActivities: nil)

if(UIDevice.current.userInterfaceIdiom == .pad){
    // This lines is for the popover you need to show in iPad
    activityViewController.isModalInPresentation = false
    activityViewController.popoverPresentationController?.sourceView = self.view//(self as! UIButton)
    
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
    
}else{
    // This lines is for the popover you need to show in iPad
    activityViewController.isModalInPresentation = true
    activityViewController.popoverPresentationController?.sourceView = (self as! UIButton)
    
    // This line remove the arrow of the popover to show in iPad
    activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
}

// Pre-configuring activity items
activityViewController.activityItemsConfiguration = [
    UIActivity.ActivityType.message
] as? UIActivityItemsConfigurationReading

// Anything you want to exclude
activityViewController.excludedActivityTypes = [
    UIActivity.ActivityType.postToWeibo,
    UIActivity.ActivityType.print,
    UIActivity.ActivityType.assignToContact,
    UIActivity.ActivityType.saveToCameraRoll,
    UIActivity.ActivityType.addToReadingList,
    UIActivity.ActivityType.postToFlickr,
    UIActivity.ActivityType.postToVimeo,
    UIActivity.ActivityType.postToTencentWeibo,
    UIActivity.ActivityType.postToFacebook,
    UIActivity.ActivityType.postToTwitter
]


self.present(activityViewController, animated: true, completion: nil)

I am trying to replicate self.dismiss when the red box area is tapped above the UIActivityViewController (as shown in the image)


Solution

  • After setting isModalInPresentation to true, controller removes support for out-of-area tap gestures.

    Solution: don't set isModalInPresentation to true.