在 UIWebView iOS 中选择图像时模态视图关闭 [英] Modal view closes when selecting an image in UIWebView iOS

查看:26
本文介绍了在 UIWebView iOS 中选择图像时模态视图关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个应用程序,其中弹出一个包含 WkWebView 的模式视图.当我想在此模态视图中上传图像并出现照片选择"时,模态视图会返回到启动它的视图控制器.

I'm currently building an app with a modal view popping up that contains a WkWebView. When I want to upload an image within this modal view and the Photo Selection appears, the modal view just dismisses back to the view controller that fired it up.

我该如何预防?

import UIKit

class PostWindow : UIViewController {

@IBAction func close(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // do stuff here
    let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 70, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
    myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "https://m.facebook.com/")!))
    self.view.addSubview(myWebView)

    self.title = "News Feed"

    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
    UIApplication.sharedApplication().statusBarHidden = false

    /*let addButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search,
    target: self,
    action: #selector(self.openSearch(_:)))
    self.navigationItem.setRightBarButtonItems([addButton], animated: true)*/
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

}

谢谢!

推荐答案

我遇到了同样的问题.我发现文件上传操作表在选择一个选项时尝试关闭两次,这导致模式也被关闭.

I encountered the same issue. I found that the file upload action sheet tries to dismiss itself twice upon selecting an option, which results in the modal being dismissed as well.

一个解决方案是将包含 webview 的 UINavigationController 子类化并覆盖 dismissViewControllerAnimated 以忽略它,除非它实际上有一个 presentedViewController.

A solution is to subclass the UINavigationController containing the webview and override dismissViewControllerAnimated to ignore it unless it actually has a presentedViewController.

像这样:

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
    if (self.presentedViewController != nil) {
        super.dismissViewControllerAnimated(flag, completion: completion)
    }
}

如果您不使用导航控制器,只需在 webview 中覆盖此方法即可.

If you're not using a navigation controller, just override this method in the webview instead.

这篇关于在 UIWebView iOS 中选择图像时模态视图关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆