顶部酒吧3D触摸窥视

我有一个UICollectionView ,在3D Touch编辑时显示’Peek’。 作为默认行为,’Peek’忽略导航栏。

但是,我确实想要显示一个条形,就像在下面显示的iMessage Peek中一样:

在此处输入图像描述

Collection View Controller和Peek View Controller都在Navigation View Controller中。

我在下面的Apple示例代码中有以下代码片段,我正在尝试修改以满足以上需求:

extension ChatTableViewController: UIViewControllerPreviewingDelegate { func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { guard let indexPath = tableView.indexPathForRow(at: location) else { return nil } let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: ChatDetailViewController.identifier) guard let chatDetailViewController = viewController as? ChatDetailViewController else { return nil } chatDetailViewController.chatItem = chatItem(at: indexPath) let cellRect = tableView.rectForRow(at: indexPath) previewingContext.sourceRect = previewingContext.sourceView.convert(cellRect, from: tableView) chatDetailViewController.isReplyButtonHidden = true return chatDetailViewController } func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { if let chatDetailViewController = viewControllerToCommit as? ChatDetailViewController { chatDetailViewController.isReplyButtonHidden = false } show(viewControllerToCommit, sender: self) } 

UPDATE

感谢Leo Natan我能够完成以上任务:

 extension ChatTableViewController: UIViewControllerPreviewingDelegate { func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { guard let indexPath = tableView.indexPathForRow(at: location) else { return nil } let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: ChatDetailViewController.identifier) guard let chatDetailViewController = viewController as? ChatDetailViewController else { return nil } chatDetailViewController.chatItem = chatItem(at: indexPath) let cellRect = tableView.rectForRow(at: indexPath) previewingContext.sourceRect = previewingContext.sourceView.convert(cellRect, from: tableView) chatDetailViewController.isReplyButtonHidden = true let navigationController = UINavigationController(rootViewController: viewController return navigationController } func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { if let chatDetailViewController = viewControllerToCommit as? ChatDetailViewController { chatDetailViewController.isReplyButtonHidden = false } show(viewControllerToCommit, sender: self) } 

但是,这会创建一个新的导航控制器。 如果我想最终使用相同的导航控制器,我可以这样做:

 func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { if let chatDetailViewController = viewControllerToCommit as? ChatDetailViewController { chatDetailViewController.isReplyButtonHidden = false } show((viewControllerToCommit as UIController).viewControllers[0], sender: self) } 

show((viewControllerToCommit as UIController).viewControllers [0],sender:self)从它的导航控制器中提取viewController。 这有什么缺点吗?

previewingContext(_:, viewControllerForLocation:) ,将视图控制器包装在UINavigationController并返回。