Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popoverPresentationController's sourceView inside didSelectRowAtIndexPath in iPad in Swift

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
        }
    }
}
like image 724
He Yifei 何一非 Avatar asked Dec 05 '22 02:12

He Yifei 何一非


2 Answers

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
                }
            }
        }
    }
}
like image 110
Marco Boschi Avatar answered May 16 '23 06:05

Marco Boschi


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)
    }
like image 44
Mike Zriel Avatar answered May 16 '23 07:05

Mike Zriel