带顶栏的 3D Touch Peek

3D Touch Peek with Top Bar

我有一个 UICollectionView3D 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)
    }

更新

多亏了 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 和 return 中。