I want to show an popoverPresentationController
when user click an cell in iPad. Here's my code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.section == 0){
if(indexPath.row == 0){
let textToShare = NSLocalizedString("SHARE_MESSAGE", comment: "Message...")
if let appStoreURL = NSURL(string: "http://www.google.com/") {
let objectsToShare = [textToShare, appStoreURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
if let popView = activityVC.popoverPresentationController {
let v = tableView as UIView
popView.sourceView = v
popView.sourceRect = v.bounds
}
}
}
}
}
It's wrong on iPad (nothing displayed on screen). So how can I solve it?
PS: Here's code for a UIButton and it's works fine on iPad (shown on screen):
@IBAction func shareButtonAction(sender: AnyObject) {
let textToShare = NSLocalizedString("SHARE_MESSAGE", comment: "Message...")
if let appStoreURL = NSURL(string: "http://www.google.com/") {
let objectsToShare = [textToShare, appStoreURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
if let popView = activityVC.popoverPresentationController {
let v = sender as! UIView
popView.sourceView = v
popView.sourceRect = v.bounds
}
}
}
You are displaying the popover from the frame of the whole tableView so I suppose it's out of the screen. Try displaying it from the frame of the cell.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.section == 0){
if(indexPath.row == 0){
let textToShare = NSLocalizedString("SHARE_MESSAGE", comment: "Message...")
if let appStoreURL = NSURL(string: "http://www.google.com/") {
let objectsToShare = [textToShare, appStoreURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
if let popView = activityVC.popoverPresentationController {
popView.sourceView = tableView
popView.sourceRect = tableView.cellForRowAtIndexPath(indexPath)!.frame
}
}
}
}
}
Swift 5
// Detect iPad
if let popoverController = actionSheet.popoverPresentationController,
let indexPath = self.tableView.indexPathForSelectedRow,
let cell = self.tableView.cellForRow(at: indexPath) {
popoverController.permittedArrowDirections = .left
popoverController.sourceView = self.tableView
popoverController.sourceRect = CGRect(x: cell.frame.width / 7, y: cell.frame.minY + 22, width: 1, height: 1)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With