当searchController处于活动状态时,3D Touch不起作用

时间:2017-10-23 23:35:59

标签: ios uisearchcontroller peek-pop

我已经使用uicollectionview实现了3D Touch,它运行良好。但是当uisearchController处于活动状态时,3D Touch无法正常工作。 uisearchController使用collectionView来显示结果。 以下帖子同样的问题: 3d Peek & Pop for search results

任何人都有同样的问题?感谢

我已经找到了解决方案:

扩展MyViewController:UISearchControllerDelegate {

func didPresentSearchController(_ searchController: UISearchController) {
    if let context = previewingContext {
    unregisterForPreviewing(withContext: context)
    previewingContext = searchController.registerForPreviewing(with: self, sourceView: self.myCollectionView)
    }
}

func didDismissSearchController(_ searchController: UISearchController) {
    if let context = previewingContext {
        searchController.unregisterForPreviewing(withContext: context)
        previewingContext = registerForPreviewing(with: self, sourceView: self.myCollectionView)
    }
}

}

1 个答案:

答案 0 :(得分:0)

花了我一段时间才能理解问题的解决方案的含义,所以我认为将其澄清一下是个好主意。

需要为视图控制器创建一个实例变量。这需要存储从registerForPreviewing(with: sourceView:)返回的预览上下文。显示标准控制器时,必须将其作为self上的方法来调用,但是显示搜索控制器时,必须将其作为(self.)searchController上的方法来调用。这就是@ user8771003提供的扩展名。 UISearchController的委托也需要设置。

我为UITableViewController创建了一些Swift 4.2,iOS 12兼容代码,以使其更易于理解,尽管与其他UIView相似。我已经使用self来增加简洁性。

import UIKit

/// View controller for table view
class TableViewController: UITableViewController {

    //
    // MARK: - Properties
    //

    /// Data to display in table view
    var data = [String]()

    /// Controller for table view search bar
    let searchController = UISearchController(searchResultsController: nil)

    /// The 3D touch peek and pop preview context for switching between table view and search controller results
    var previewingContext: UIViewControllerPreviewing?

    //
    // MARK: - Life cycle methods
    //

    /// Sets up the table view
    override func viewDidLoad() {
        super.viewDidLoad()

        configureSearchBar()
        setupPeekAndPop()
        refreshData()

        self.tableView.reloadData()
    }

    /// Configures behaviour for search bar on older devices
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if #available(iOS 11.0, *) {

        } else {
            self.searchController.dismiss(animated: false, completion: nil)
        }
    }

    //
    // MARK: - Table view data source
    //

    // ...

    //
    // MARK: - Navigation
    //

    // ...

    //
    // MARK: - Private methods
    //

    /// Reloads the data for the table view
    /// - Parameter query: Search query to filter the data
    private func refreshData(query: String = "") {
        // ...
    }

    /// Configures the search bar
    private func configureSearchBar() {
        self.searchController.searchResultsUpdater = self
        self.searchController.searchBar.placeholder = "Search"
        self.searchController.delegate = self

        if #available(iOS 11.0, *) {
            self.searchController.obscuresBackgroundDuringPresentation = false
            self.navigationItem.searchController = self.searchController
            self.definesPresentationContext = true
        } else {
            self.searchController.dimsBackgroundDuringPresentation = false
            self.searchController.hidesNavigationBarDuringPresentation = false
            self.tableView.tableHeaderView = self.searchController.searchBar
        }
    }

}

// MARK: - Search results extension

/// Manages the extension for the `UISearchResultsUpdating` protocol to implement the search bar
extension TableViewController: UISearchResultsUpdating {

    /// Updates the table view data when the search query is updated
    func updateSearchResults(for searchController: UISearchController) {
        let query = self.searchController.searchBar.text!
        refreshData(query: query)
        self.tableView.reloadData()
    }

}

// MARK: - Peek and pop extension

/// Managess the extension for the `UIViewControllerPreviewingDelegate` protocol to implement peek and pop
extension TableViewController: UIViewControllerPreviewingDelegate {

    //
    // MARK: - Public methods
    //

    /// Manages the previwing of the destination view controller for peeking
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        guard let indexPath = self.tableView?.indexPathForRow(at: location) else { return nil }
        guard let cell = self.tableView?.cellForRow(at: indexPath) else { return nil }

        guard let detailVC = storyboard?.instantiateViewController(withIdentifier: "DestinationStoryboardIdentifier") as? DestinationViewController else { return nil }

        let item = self.data[indexPath.row]
        detailVC.item = item
        detailVC.preferredContentSize = CGSize(width: 0.0, height: 0.0)
        previewingContext.sourceRect = cell.frame

        return detailVC
    }

    /// Manages the showing of the destionation view controller for popping
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        show(viewControllerToCommit, sender: self)
    }

    //
    // MARK: - Private methods
    //

    /// Registers for peek and pop if device is 3D touch enabled
    /// - Note: Should be called in `viewDidLoad()`
    private func setupPeekAndPop() {
        if traitCollection.forceTouchCapability == .available {
            self.previewingContext = self.registerForPreviewing(with: self, sourceView: view)
        }
    }

}

// MARK: - Peek and pop on search results

/// Manages the extension for the `UISearchControllerDelegate` for implementing peek and pop on search results
extension TableViewController: UISearchControllerDelegate {

    /// Switches previewing context for peek and pop to search controller results
    func didPresentSearchController(_ searchController: UISearchController) {
        if let context = self.previewingContext {
            self.unregisterForPreviewing(withContext: context)
            self.previewingContext = self.searchController.registerForPreviewing(with: self, sourceView: view)
        }
    }

    /// Switches previewing context for peek and pop to table view
    func didDismissSearchController(_ searchController: UISearchController) {
        if let context = self.previewingContext {
            self.searchController.unregisterForPreviewing(withContext: context)
            self.previewingContext = self.registerForPreviewing(with: self, sourceView: view)
        }
    }

}