ios – UIImagePickerController&view不在窗口层次结构中

前端之家收集整理的这篇文章主要介绍了ios – UIImagePickerController&view不在窗口层次结构中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的 Swift项目,其中:

ViewController A(类ViewController:UIViewController)呈现ViewController B – (类WebViewController:UIViewController,WKNavigationDelegate,CLLocationManagerDelegate,WKScriptMessageHandler).

ViewController B本质上只是一个WKWebView,这里是它的viewDidLoad():

let contentController = WKUserContentController();
contentController.addScriptMessageHandler(
    self,name: "eventHandler"
)

let config = WKWebViewConfiguration()
config.userContentController = contentController

webView = WKWebView(frame: CGRectZero,configuration: config)
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = false
view = webView

因此,当ViewController B出现时,我基本上只在整个屏幕上有一个Web浏览器.

我遇到的问题是,如果用户(在网页上)点击“文件上传/文件选择器”,首先我看到:

Passed in type public.item doesn’t conform to either public.content or
public.data. If you are exporting a new type,please ensure that it
conforms to an appropriate parent type. the behavior of the
UICollectionViewFlowLayout is not defined because the item width must
be less than the width of the UICollectionView minus the section
insets left and right values,minus the content insets left and right
values.

The relevant UICollectionViewFlowLayout instance is
<_UIAlertControllerCollectionViewFlowLayout: 0x12e30f270>,and it is
attached to ;
animations = { bounds.origin=;
bounds.size=;
position=; }; layer = ; contentOffset: {0,0}; contentSize: {0,0}> collection
view layout: <_UIAlertControllerCollectionViewFlowLayout:
0x12e30f270>.

然后,如果用户从iOS选项列表中选择“拍摄照片或视频”或“照片库”,我会得到:

Warning: Attempt to present UIImagePickerController: 0x12d192200 on
My.WebViewController: 0x12e2883e0 whose view is not in the window
hierarchy!

结果是:

>照片库选择器//相机应用程序永远不会出现.
> ViewController B由于某种原因经历了动画解雇.

有没有人有什么建议?我试图提供相关代码,但如果需要我可以粘贴更多代码.谢谢.

– 更新——–

好的,经过更多的研究,实际上还有两个不同的事情:

>第一个错误是移动浏览器处理“文件选择器”的方式. See here ……宏伟的计划,没什么大不了的.
>第二个错误尝试呈现UIImagePickerController …… – 两者中更令人衰弱的 – 更加丑陋.

如果具有WKWebView的ViewController从另一个ViewController呈现,那么当用户试图“选择”图像(来自库或相机)时,iOS将尝试从您的WKWebView ViewController呈现UIImagePicker,这就是您想要的.

不幸的是,由于我还不知道的原因,用户试图“挑选”图像的行为也会导致iOS解雇所呈现的视图控制器……在这种情况下,它是您的WKWebView控制器:(如此,WKWebView ViewController解散,因此当它试图呈现UIImagePicker时它不在窗口层次结构中 – 因此,错误.

我的解决方案(非常hacky,但适用于我的特定用例),如下所示;我希望这可以帮助别人:

>实例化WKWebView ViewController(ViewController B)
>将VC B设置为应用程序的根视图控制器
>从ViewController A呈现VC B.
>解雇VC A.

这基本上使得WKWebView无法被解雇,避开了问题.

let storyboard = UIStoryboard(name: "Main",bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Browser") as! WebViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc

self.presentViewController(vc,animated: false,completion: { () -> Void in
    self.dismissViewControllerAnimated(false,completion: nil)
})

解决方法

如果你当前正在viewDidLoad(){}函数中执行int,那么尝试将代码移动到覆盖viewDidAppear(动画:Bool){}我今天遇到了同样的问题并修复了它.
原文链接:https://www.f2er.com/iOS/331654.html

猜你在找的iOS相关文章