3

我试图为UIContextMenuConfigurationtableView的基于应用程序实现一个新的。我还添加了一个新的委托方法,如下所示。它运行良好,但我想添加一个类似于原生 iOS 13 应用程序的功能。单击预览以显示目的地!

我的问题是:是否有可能实现这样的功能?我正在使用 Xcode 11.1 GM 和 Swift 5.1

override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    let provider = UIContextMenuConfiguration.init(identifier: indexPath as NSCopying, previewProvider: { () -> UIViewController? in
        let vc = ViewController.init()
        return vc
    }) { (elements) -> UIMenu? in
        let addToList = UIAction.init(title: "Add to list") { (action) in
            self.performSegue(withIdentifier: "id", sender: self)
        }
        addToList.image = UIImage.init(systemName: "plus")
        return UIMenu.init(title: "", image: nil, identifier: nil, options: .destructive, children: [addToList])
    }
    return provider
}

override func tableView(_ tableView: UITableView, willCommitMenuWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
    if let vc = animator.previewViewController {
        self.show(vc, sender: self)
    }
}
4

1 回答 1

2

我发现了我的问题,这是我的错。我使用了错误的方法,所以我只是用正确的方法替换。我已将该willCommitMenuWithAnimator方法替换为willPerformPreviewActionForMenuWith,并且运行良好:

override func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
    animator.preferredCommitStyle = .pop
    animator.addCompletion {
        guard let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController") as? ViewController else { return }
        // 1. Present option
        self.present(vc, animated: true, completion: nil)

        // 2. Push option
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
于 2019-10-27T03:45:07.027 回答