在WkWebView iOS中选择图像时,模态视图关闭

Flo*_*ers 7 viewcontroller modal-view swift

我正在构建一个包含WkWebView的弹出模式视图的应用程序.当我想在此模态视图中上传图像并且出现"照片选择"时,模态视图只会将其解除回发射它的视图控制器.

我怎么能防止这种情况?

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
}
Run Code Online (Sandbox Code Playgroud)

}

谢谢!

小智 13

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

一个解决方案是子类UINavigationController包含webview和override dismissViewControllerAnimated来忽略它,除非它实际上有一个presentedViewController.

像这样:

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

如果您没有使用导航控制器,请改为在webview中覆盖此方法.