在 UIWebView 中选择图像时模态视图关闭 iOS
Modal view closes when selecting an image in UIWebView iOS
我目前正在构建一个包含 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
}
}
谢谢!
我遇到了同样的问题。我发现文件上传操作 sheet 会在选择一个选项时尝试自行关闭两次,这会导致模式也被关闭。
一个解决方案是将包含 webview 的 UINavigationController
子类化并覆盖 dismissViewControllerAnimated
以忽略它,除非它实际上有一个 presentedViewController
。
像这样:
override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
if (self.presentedViewController != nil) {
super.dismissViewControllerAnimated(flag, completion: completion)
}
}
如果您不使用导航控制器,只需在 webview 中覆盖此方法即可。
它也适用于非 NavigationController / ViewController 仅以这种方式:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil){
if(self.presentedViewController != nil)
{
super.dismiss(animated: flag, completion: completion)
}
}
对于那些使用 func dismiss()
无法修复的人,您可以使用这个技巧:
extension UIImagePickerController {
open override func viewDidLoad() {
super.viewDidLoad()
self.modalPresentationStyle = .overFullScreen // this will turn off dismiss of webView when imagePicker presents
}
}
我目前正在构建一个包含 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
}
}
谢谢!
我遇到了同样的问题。我发现文件上传操作 sheet 会在选择一个选项时尝试自行关闭两次,这会导致模式也被关闭。
一个解决方案是将包含 webview 的 UINavigationController
子类化并覆盖 dismissViewControllerAnimated
以忽略它,除非它实际上有一个 presentedViewController
。
像这样:
override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
if (self.presentedViewController != nil) {
super.dismissViewControllerAnimated(flag, completion: completion)
}
}
如果您不使用导航控制器,只需在 webview 中覆盖此方法即可。
它也适用于非 NavigationController / ViewController 仅以这种方式:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil){
if(self.presentedViewController != nil)
{
super.dismiss(animated: flag, completion: completion)
}
}
对于那些使用 func dismiss()
无法修复的人,您可以使用这个技巧:
extension UIImagePickerController {
open override func viewDidLoad() {
super.viewDidLoad()
self.modalPresentationStyle = .overFullScreen // this will turn off dismiss of webView when imagePicker presents
}
}