我有一个 UICollectionView ,在 3D Touch 'ed时显示'Peek'。作为默认行为,'Peek'忽略导航栏。
但是,我确实想要显示一个栏,就像下面显示的 iMessage Peek 一样:
Collection View Controller& Peek 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。这有什么缺点吗?
答案 0 :(得分:1)
在previewingContext(_:, viewControllerForLocation:)
中,将视图控制器包装在UINavigationController
中并返回。