搜索结果的 3d Peek & Pop

3d Peek & Pop for search results

我使用搜索栏来过滤我的 Table 视图。我在显示可搜索内容的同一个视图控制器中显示搜索结果。它工作正常。除了一件事 - 3d Peek and Pop 不适用于搜索结果。 我确实尝试了 Storyboard segue 中的“Peek & Pop”复选框。我确实在 TableViewController 中尝试过编程 peek & pop。结果相同:peek & pop 适用于 table 查看单元格,但不适用于搜索结果单元格。

这是搜索和查看及弹出代码片段:

    // Add a search bar
    searchController = UISearchController(searchResultsController: nil)
    tableView.tableHeaderView = searchController.searchBar
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    self.tableView.contentOffset = CGPoint(x: 0, y: searchController.searchBar.frame.height)

    // Register for preview
    if traitCollection.forceTouchCapability == .available {
        previewingContext = registerForPreviewing(with: self, sourceView: tableView)
    }


    func updateSearchResults(for searchController: UISearchController) {
        if let searchText = searchController.searchBar.text {
            filterContent(for: searchText)
            tableView.reloadData()
        }
    }

    // MARK: UIViewControllerPreviewingDelegate methods
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {        
        guard let tableView = previewingContext.sourceView as? UITableView,
            let indexPath = tableView.indexPathForRow(at: location),
            let cell = tableView.cellForRow(at: indexPath),
            let detailVC = storyboard?.instantiateViewController(withIdentifier: "AlarmViewController") as? AlarmViewController
        else { return nil }
        detailVC.alarm = alarms[indexPath.row]
        previewingContext.sourceRect = cell.frame       
        return detailVC
    }

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

有没有人知道如何为搜索结果实现 peek & pop?

我想主要问题是如何注册预览UISearchController?..

我成功了。有几个 'new kid on the block' 错误。正确代码:

// Add a search bar
searchController = UISearchController(searchResultsController: nil)
searchController.loadViewIfNeeded()
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
self.tableView.contentOffset = CGPoint(x: 0, y: searchController.searchBar.frame.height)
// Hide search bar on detailed view and preserve search results
    self.definesPresentationContext = true

// Register for preview
if traitCollection.forceTouchCapability == .available {
    previewingContext = registerForPreviewing(with: self, sourceView: tableView)
}

func updateSearchResults(for searchController: UISearchController) {
    if let searchText = searchController.searchBar.text {
        filterContent(for: searchText)
        tableView.reloadData()
    }
}

// MARK: UIViewControllerPreviewingDelegate methods
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {        
    guard let tableView = previewingContext.sourceView as? UITableView,
        let indexPath = tableView.indexPathForRow(at: location),
        let cell = tableView.cellForRow(at: indexPath),
        let detailVC = storyboard?.instantiateViewController(withIdentifier: "AlarmViewController") as? AlarmViewController
    else { return nil }
    detailVC.alarm = (searchController.isActive) ? searchResults[(indexPath.row)] : alarms[indexPath.row]
    previewingContext.sourceRect = cell.frame       
    return detailVC
}

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

简而言之: 1) 为 searchController 添加委托 2) 如果 searchController.isActive

在 UIViewControllerPreviewingDelegate 中使用 searchResults

希望对大家有所帮助。