Attempt to present <UIImagePickerController > on <UINavigationController: > whose view is not in ...

问题描述

当从一个controller present 到 webviewcontroller,加载的html弹框选取照片时,选取之后并没有弹出imagepickcontroller,而且当前的webviewcontroller也会dismiss掉,并且控制台输出错误内容

Warning: Attempt to present <UIImagePickerController: 0x7fe7fc008600> on <Test.XYNavigationViewController: 0x7fe7fc860a00> whose view is not in the window hierarchy!

Simulator Screen Shot - iPhone X - 2018-10-25 at 17.06.31.png

在我们重写了dismiss方法

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    print("test")
    super.dismiss(animated: flag, completion: completion)
}

会发现test输出了两次


Snip20181025_1.png

UIWebview使UIAlertcontrollerUIWebviewviewcontroller上显示以显示相机/现有/取消。当用户选择选项时,UIAlertcontroller意味着被释放并且UIImagepickercontroller被呈现。然而,对UIAlertcontroller的释放似乎调用了两次。第一次是好的,因为presentviewcontroller =UIAlertcontroller,但第二次presentviewcontroller属性为nil,这导致dismissviewcontroller animated方法杀死UIWebviewviewcontroller

解决方法:

  • 方法一
    使用push到webviewcontroller,这是最简单的方法了

  • 方法二

在自定义的UINavigationcontroller做些操作

class XYNavigationViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.view.window?.rootViewController = self
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    print("test")
    if let vc = self.presentedViewController {
        // don't bother dismissing if the view controller being presented is a doc/image picker
        if !vc.isKind(of: UIDocumentMenuViewController.self) || !vc.isKind(of:UIImagePickerController.self) {
            super.dismiss(animated: flag, completion:completion)
        }
    }

}

}

参考链接地址:https://stackoverflow.com/questions/25942676/ios-8-sdk-modal-uiwebview-and-camera-image-picker

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 统一弹登录页面,有的是在push出来的页面弹登录页面,有的是在present页面弹登录页面 说明 : 例如:A p...
    左岸花不开阅读 1,432评论 0 1
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,589评论 2 4
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,009评论 3 119
  • 我决定来一场远行 在那个梦湿的清晨 我收拾起行囊 却发现囊中羞涩 可即便如此 我也不会回头 那样只会令别人更加耻笑...
    十月城池阅读 327评论 5 0
  • 伊始 日晷转动 回旋梯下 花与叶在飞舞 黎明前 秋千默默摆动 先民的祈祷 鬼神在歌颂 这是亡者的舞曲 也是来自旷古...
    邵尘阅读 193评论 0 0