Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 views
in Technique[技术] by (71.8m points)

swift - Modal view closes when selecting an image in UIWebView iOS

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.

How can I prevent that?

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
}

}

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

Like so:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...