有关简单演示,请参阅https://gfycat.com/LightheartedUnimportantKingfisher或https://github.com/NathanJang/iOS-Preview-Demo/blob/master/Preview%20Demo/ViewController.swift。
我想查看包含在UINavigationController
中的视图控制器,以便用户可以看到正在查看的视图控制器的标题。然而,内容"跳跃"偷看时,并在观看后不久改变位置。我该如何解决这个问题?
我创建了一个~15行演示,以在上面的链接中显示这种效果。以下是我的懒惰委托方法:
extension ViewController: UIViewControllerPreviewingDelegate {
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let detailViewController = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") else { return nil }
let navigationController = UINavigationController(rootViewController: detailViewController)
return navigationController
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
guard let detailViewController = (viewControllerToCommit as? UINavigationController)?.visibleViewController else { return }
navigationController?.pushViewController(detailViewController, animated: false)
}
}
我找到的唯一解决方法是在返回包装导航控制器之前调用viewWillAppear
,但显然这是不好的做法。