popoverPresentationController 的 sourceView inside iPad in Swift didSelectRowAtIndexPath

popoverPresentationController's sourceView inside didSelectRowAtIndexPath in iPad in Swift

我想在用户单击 iPad 中的单元格时显示 popoverPresentationController。这是我的代码:

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
                }
            }
        }
    }
}

iPad 错误(屏幕上没有显示)。那我该如何解决呢?

PS:这是 UIButton 的代码,它在 iPad 上运行良好(显示在屏幕上):

@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
        }
    }
}

您正在从整个 tableView 的框架中显示弹出窗口,所以我想它在屏幕之外。尝试从单元格的框架显示它。

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)
    }