我想在用户单击 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
}
}
}
}
}
关于ios - popoverPresentationController sourceView inside didSelectRowAtIndexPath on iPad in Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742101/