Getting a sourceRect for UIActivityViewController from a UIDocumentBrowserViewController - ios

I am trying to add a custom activity to my UIDocumentBrowserController long-tap menu - a UIActivityViewController. The default share activity, when selected, is pointing nicely to the icon of the file that the user just long-tapped.
How can I get the sourceRect so that my activity could be presented in a similar way?
let shareBundle = UIDocumentBrowserAction(identifier:
"com.mycompany.myapp.shareBundle", localizedTitle: "Share with Data",
availability: [.menu], handler: { urls in
if urls.count > 0 {
let objectsToShare: [URL] = [urls]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
activityVC.popoverPresentationController?.sourceRect = // That's the problem!
self.present(activityVC, animated: true, completion: nil)
}
})

Looks like it is currently not possible to get the view of the selected document.
For now I ended up just positioning the ActivityView at the center of the screen and disabling the arrows:
if let popOverController = activityVC.popoverPresentationController {
popOverController.sourceView = self.view
popOverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popOverController.permittedArrowDirections = []
}

Related

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)
After setting isModalInPresentation to true, controller removes support for out-of-area tap gestures.
Solution: don't set isModalInPresentation to true.

Swift3: Crash on presenting pop over

let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
obj.delegate = self
obj.modalPresentationStyle = .popover
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
self.present(obj, animated: true, completion: nil)
On setting up breakpoints, debugger goes good till last line. After that, it directly goes to AppDelegate class first line.
I have set exception break point properly. Where I might be making a mistake? Is it related to sourceView for popoverPresentationController? I am not sure.
What I want to do is set up the popoverPresentationController in center. Any help?
EDIT:
I added the sourceView to the code like following & now it's working:
obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)
However, it's not in the center of the screen. Putting screen shot for reference. How do I make it to the center and remove the direction arrow?
After doing the following code changes I was able to make it work.
let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
obj.delegate = self
obj.modalPresentationStyle = .popover
obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0)
obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)
self.present(obj, animated: true, completion: nil)
Thank You all for your efforts.
You have to use sourceView in conjunction with sourceRect to provide anchor point for pop over, like following:
obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)
Also, If you don't want the anchor point arrow to be there, then use:
obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0)
It will make your pop over appear in center with no arrow/anchor point.
Try this:
let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
obj.delegate = self
obj.modalPresentationStyle = .overCurrentContext
self.navigationController?.present(obj, animated: false, completion: {
})

Swift: I want add labels as overlay to an Image and then I want to share the Image to Social Network

Swift: I want add labels as overlay to an Image and then I want to share the Image to Social Network.
This is the code sharing the image to social network ..
#IBAction func share(sender: AnyObject) {
let firstActivityItem = "Text you want"
let secondActivityItem : NSURL = NSURL(string: "http//:www.google.com")!
// If you want to put an image
let image : UIImage = scannedImage.image!
textToImage("gsdghdhfjhfhgdhdh", inImage: scannedImage.image!, atPoint: CGPointMake(20, 20))
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)
// This lines is for the popover you need to show in iPad
activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)
// This line remove the arrow of the popover to show in iPad
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
// Anything you want to exclude
activityViewController.excludedActivityTypes = [
]
self.presentViewController(activityViewController, animated: true, completion: nil)
}

Popover presentation in iOS 9

I want to Display a viewController in Popover in iPad . But this line
self.poc = UIPopoverController(contentViewController: sec) deprected in iOS 9
let sec:PopView=PopView(nibName:"PopView",bundle:nil)
self.poc = UIPopoverController(contentViewController: sec)
poc!.delegate=self
self.poc!.presentPopoverFromRect( CGRect(x: 150, y: 222, width: 50, height: 30), inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Right, animated: true)
You need to use UIPopoverPresentationController :
let sec = UIViewController(nibName:"PopView",bundle:nil)
sec.modalPresentationStyle = .Popover
if let secPopoverPresentationController = sec.popoverPresentationController{
secPopoverPresentationController.sourceView = yourSourceView
secPopoverPresentationController.permittedArrowDirections = [.Any]
//Optionally configure other things like sourceRect , delegate ...
}
presentViewController(sec, animated: true, completion: nil)
Hope this helps.

UIDocumentInteractionController PopOver on iPad

When you display a UIActionSheet on the iPad, it appears as a centered UIPopover with no arrow.
I have a UIDocumentInteractionController, and when I call presentOpenInMenuFromRect:inView:animated:, I am looking for the same behavior with the popover that appears there. Anyway to do that? I know you can have no arrow on a popover by setting the PermittedArrowDirections to 0, but that doesn't seem to be an option here.
You can use UIActivityController instead
let url = #Your File URL#
var activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
activityViewController.popoverPresentationController?.sourceView = view
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: view.frame.midX, y: view.frame.midY,
width: 0, height: 0)

Resources