2015-09-04 70 views
1

我想添加一個UIView的內部WKWebView創建WKWebView,在一個UIView

的代碼似乎很好地工作(在頁面加載的範圍是好的委託打印,並沒有錯誤消息) 但視圖(稱爲ViewForStuffInWeb)保持空白,沒有什麼可以解釋爲什麼?

import UIKit 
import WebKit 

class ViewController: UIViewController ,WKNavigationDelegate { 


@IBOutlet var ViewForStuffInWeb: UIView! 

var webView = WKWebView() 
let request = "https://www.google.fr" 



override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    self.automaticallyAdjustsScrollViewInsets = false 
    let config = WKWebViewConfiguration() 
    webView = WKWebView(frame: ViewForStuffInWeb.bounds ,configuration : config) 
    webView.navigationDelegate = self 
    ViewForStuffInWeb = webView 
    println("webview : frame :\(webView.frame) , bounds : \(webView.bounds)") 
    requesting(request) 
    } 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 
} 

func requesting (request :String) { 
    if let url = NSURL(string: request) { 
     webView.loadRequest(NSURLRequest(URL: url)) 
    } 

} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { 
    println("decide policy action") 
    decisionHandler(WKNavigationActionPolicy.Allow) 
} 
func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) { 
    println("decide policy response") 
    decisionHandler(WKNavigationResponsePolicy.Allow) 
} 
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) { 
    println("commit navigation") 
} 
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) { 
    println("fail in didFailNavigation \(error)") 
} 
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) { 
    println("fail in didFailProvisionalNavigation \(error)") 
} 
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { 
    println("finish navigation") 
} 

感謝閱讀

回答

1

你永遠不添加WKWebView到視圖層次結構。在你viewDidAppear,該代碼真的應該移動到viewDidLoad,你可能要更換:

ViewForStuffInWeb = webView 

有:

ViewForStuffInWeb.addSubview(webView) 

雖然目前還不清楚什麼ViewForStuffInWeb實際上是。如果該視圖存在於您的筆尖並且它已連接,上述操作纔有效。