I have implemented 3D Touch with uicollectionview, it worked fine. But when the uisearchController is active, the 3D Touch doesn't work. The uisearchController uses the collectionView to show the results. same problem with the following post: 3d Peek & Pop for search results
Anyone has the same problem? Thanks
I have figured out the solution:
extension 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)
}
}
}
It took me a while to understand what the solution in the question meant, so I thought that it would be a good idea to clarify it a bit.
An instance variable needs to be create for the view controller. This needs to store the previewing context returned from
registerForPreviewing(with: sourceView:). When the standard controller is displayed, it must be called as a method onself, but when the search controller is displayed, it must be called as a method on(self.)searchController. That is what the extension provided by @user8771003 does. The delegate for theUISearchControlleralso needs to be set.I created some Swift 4.2, iOS 12 compatible code for a
UITableViewControllerto make it easier to understand, although it will very similar for any otherUIView. I have made use ofselfto increase brevity.