ios - 在 iOS 中将 Peek 和 Pop 与 Popover 相结合

标签 ios storyboard

是否可以在 iOS 中将 peek 和 pop 与 popover 结合起来?

我想在支持 3D Touch 的 iPhone 上使用 peek and pop,同时在 iPad 上使用 popover。

当我尝试将其合并到 Storyboard 中时,出现错误“无法编译连接”。

enter image description here

最佳答案

我自己找到了答案。

问题在于,PopOver 的 anchor 指向动态创建的原型(prototype)单元格,因此系统无法确定哪个单元格是 anchor 。

因此解决方案如下:

  1. 将弹出窗口的 anchor 设置为表格 View
  2. 从 Storyboard segue 中删除 peek & pop 功能,因为我们需要在代码中执行此操作。
  3. 转到您的 UITableViewDataSource(在我的例子中是 View Controller )并使用单元格作为源 View 将以下内容添加到您的 cellForRowAt():
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier", for: indexPath) as! MyTableViewCell
    let item = items[indexPath.row]
    // Pass the item here...
    registerForPreviewing(with: self, sourceView: cell) // <== Add this
    return cell
}
  1. 之后,您必须像这样遵守协议(protocol) UIViewControllerPreviewingDelegate:
extension ListeningVC: UIViewControllerPreviewingDelegate {
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        guard let indexPath = tableView.indexPathForRow(at: location) else {
            return nil
        }
        // get the item you want to pass using the index path
        let item = items[indexPath.row]
        // get the destination view controller
        let destination = UIStoryboard(name: "...", bundle: nil).instantiateInitialViewController()
        // Pass the item here...
        return destination // return the destination view controller
    }

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        // either present the view controller or add it to your navigation controller here
        present(viewControllerToCommit, animated: true)
    }
}
  1. 如果需要,您可以将 anchor (源 View )稍微居中:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier {
    case "mySegueIdentifier":
        guard let cell = sender as? MyTableViewCell else { return }
        guard let indexPath = tableView.indexPath(for: cell) else { return }
        let item = items[indexPath.row]
        let destination = segue.destination
        // Pass the item here...
        if let popOver = segue.destination.popoverPresentationController {
            // set the cell as source view
            let origin = CGPoint(x: 100, y: cell.frame.origin.y)
            let size = CGSize(width: 200, height: cell.frame.height)
            popOver.sourceView = tableView
            popOver.sourceRect = CGRect(origin: origin, size: size)
        }
    default:
        break
    }
}
  1. 很高兴这可行,并对这个答案投赞成票。 :)

关于ios - 在 iOS 中将 Peek 和 Pop 与 Popover 相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54746840/

相关文章:

iphone - Storyboard和 xib 在同一个项目中使用

ios - 为了能够在 View 与 Storyboard 场景之间按住 Ctrl 键并拖动一个 segue,先决条件是什么

wpf - 从 Storyboard 中获取框架元素

iphone - 推送通知警报文本的最大长度是多少?

iphone - 如何在 objective-c iphone中将内容写入文本文件

ios - 在 tableViewCell 上列出 firebase 子节点时删除值

ios - 如何像 Path Talk 应用程序一样编写设置 View Controller ?

ios - 如何在导航栏下扩展 View 后更正 View 的中心

ios - 有没有办法用一个 UICloudSharingController 创建多个 CKShares?

ios - 启动失败。该应用程序无法在 xamarin 上启动