【问题标题】:Modal view closes when selecting an image in UIWebView iOS在 UIWebView iOS 中选择图像时模式视图关闭
【发布时间】:2016-05-22 22:30:15
【问题描述】:

我目前正在构建一个应用程序,它会弹出一个包含 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
}

}

谢谢!

【问题讨论】:

    标签: swift viewcontroller modal-view


    【解决方案1】:

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

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

    像这样:

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

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

    【讨论】:

      【解决方案2】:

      对于那些使用func dismiss() 无法解决问题的人,您可以使用这个技巧:

      extension UIImagePickerController {
        open override func viewDidLoad() {
          super.viewDidLoad()
          self.modalPresentationStyle = .overFullScreen // this will turn off dismiss of webView when imagePicker presents
        }
      }
      
      

      【讨论】:

      • 天才。这行得通。我用自适应代表尝试了各种各样的事情。我正在编写 SwiftUI 代码,它运行良好。你应该有更多的赞成票!!谢谢!!
      【解决方案3】:

      它也仅适用于非 NavigationController / ViewController:

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

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        相关资源
        最近更新 更多