2016-05-22 80 views
6

我目前正在構建一個應用程序,彈出包含WkWebView的模式視圖。當我想要在此模式視圖中上傳圖像並且出現照片選擇時,模式視圖會退回到啓動它的視圖控制器。在WkWebView中選擇圖像時模態視圖關閉iOS

我該如何預防?

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 
} 

}

謝謝!

回答

9

我遇到了同樣的問題。我發現文件上傳操作表在嘗試選擇一個選項後會自動退出兩次,導致模式被解除。

一個解決方案是繼承包含webview的UINavigationController並覆蓋dismissViewControllerAnimated忽略它,除非它實際上有一個presentedViewController

像這樣:

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

如果你不使用導航控制器,只是覆蓋在網頁視圖,而不是此方法。

0

它只有通過這種方式也適用於非NavigationController /視圖控制器:

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