Peek-and-Pop UIView 显示 SFSafariViewController

Peek-and-Pop UIView to show SFSafariViewController

我有一个 UITableView 并且在每个单元格中添加了一个 UIView 作为子视图。我目前拥有的是一个 UITapGestureRecognizer,每当我单击其中一个 UIViews 时,它就会打开 In-App-Safari。但是,我正在努力实现 Apples sample code 的 peek 和 pop 功能以进行强制触摸。我的代码如下所示:

 @IBOutlet var mainTableView: UITableView!

viewDidLoad():

 registerForPreviewing(with: self as! UIViewControllerPreviewingDelegate, sourceView: mainTableView)

viewDidLoad() 之后:

 func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
            // First, get the index path and view for the previewed cell.
        guard let indexPath = tableView.indexPathForRow(at: location),
              let cell = tableView.cellForRow(at: indexPath)
              else { return nil }

 //Enable blurring of other UI elements, and a zoom in animation while peeking.

 previewingContext.sourceRect = cell.frame

 //Create and configure an instance of the color item view controller to show for the peek.

 guard let url = someURL else { return nil }
            let vc = SFSafariViewController(url: url)
            vc.delegate = self as SFSafariViewControllerDelegate

            return vc
        }

        func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) 
{

//Push the configured view controller onto the navigation stack.

 navigationController?.pushViewController(viewControllerToCommit, animated: true)
}

Xcodereturns等错误

Received memory pressure event 4 vm pressure 1

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController previewingContext:viewControllerForLocation:]: unrecognized selector sent to instance

我还没有找到在按下 UIView 时查看和弹出网页内容的解决方案。

好的,所以我发现我只是忘了将 UIViewControllerPreviewingDelegate 添加到我的 class:

class MyViewController: UITableViewController, UIViewControllerPreviewingDelegate, SFSafariViewControllerDelegate

peek 使用规定的方法按预期工作,但要使 pop 工作,我必须调整我的最后一个函数:

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    show(viewControllerToCommit, sender: self)
}