在 UIView 中创建 WKWebView
Create a WKWebView in a UIView
我正在尝试在 UIView 中添加一个 WKWebView,
代码似乎工作正常(页面已加载,边界良好,代理打印消息没有错误)
但是视图(称为 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")
}
感谢阅读
您永远不会将 WKWebView
添加到视图层次结构中。在您的 viewDidAppear
中,哪些代码真正应该移动到 viewDidLoad
,您可能想要替换:
ViewForStuffInWeb = webView
与:
ViewForStuffInWeb.addSubview(webView)
虽然不清楚ViewForStuffInWeb
究竟是什么。仅当您的笔尖中存在该视图并且已连接时,以上内容才有效。
我正在尝试在 UIView 中添加一个 WKWebView,
代码似乎工作正常(页面已加载,边界良好,代理打印消息没有错误) 但是视图(称为 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")
}
感谢阅读
您永远不会将 WKWebView
添加到视图层次结构中。在您的 viewDidAppear
中,哪些代码真正应该移动到 viewDidLoad
,您可能想要替换:
ViewForStuffInWeb = webView
与:
ViewForStuffInWeb.addSubview(webView)
虽然不清楚ViewForStuffInWeb
究竟是什么。仅当您的笔尖中存在该视图并且已连接时,以上内容才有效。