我的 View 层次结构如下所示:
- (root) PRSBaseViewController - 一个 UIViewController 子类 - 有 subview Controller
- (呈现)PRSModalWebViewController - UINavigationController 子类
- (推送,动画:否)PRSWebViewController - 一个 UIViewController 子类 - WKWebView 是一个 subview 。
- (呈现)PRSModalWebViewController - UINavigationController 子类
当我尝试长按 WebView 中的链接时出现错误:
Warning: Attempt to present <WKActionSheet: 0x127520d10> on <PRSBaseViewController: 0x1275117f0> whose view is not in the window hierarchy!
而不是使用 presentViewController:animated:completion
呈现导航而是使用 addChildViewController:
dance 将 View Controller 添加到层次结构中。我没有收到任何错误,这很奇怪。
有谁知道可能导致 View 层次结构问题的原因?
更新:我做了一个 Gist of all my classes
最佳答案
我遇到过类似的行为 - 但也没有弄清楚这种情况是如何发生的或为什么会发生。
这是我的解决方法。由于 WKWebView 正在调用我的 RootViewController,我通过覆盖 RootViewController 的 presentViewController:animated:completion: 来处理这个问题 - 如果 RootViewController 已经有一个 presentedViewController,那么它将消息转发到该 Controller 。它似乎解决了警告消息,并在模态视图中使用 WKWebView 时为我提供了长按菜单。
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
// Unsure why WKWebView calls this controller - instead of it's own parent controller
if (self.presentedViewController) {
[self.presentedViewController presentViewController:viewControllerToPresent animated:flag completion:completion];
} else {
[super presentViewController:viewControllerToPresent animated:flag completion:completion];
}
}
或者在 swift 中:
override func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) {
if presentedViewController != nil {
// Unsure why WKWebView calls this controller - instead of it's own parent controller
presentedViewController?.presentViewController(viewControllerToPresent, animated: flag, completion: completion)
} else {
super.presentViewController(viewControllerToPresent, animated: flag, completion: completion)
}
}
关于ios - 当 WKWebView 尝试呈现 WKActionSheet 时,长按会导致错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767785/