菜鸟教程小白 发表于 2022-12-13 10:23:23

ios - 在 UIView 中创建 WKWebView


                                            <p><p>我正在尝试在 UIView 中添加 WKWebView ,</p>

<p>代码似乎工作正常(页面加载边界很好,委托(delegate)打印消息没有错误)
但是 View (称为 ViewForStuffInWeb )保持空白,其中没有任何内容
有人能解释一下为什么吗?</p>

<pre><code>import UIKit
import WebKit

class ViewController: UIViewController ,WKNavigationDelegate{


@IBOutlet var ViewForStuffInWeb: UIView!

var webView = WKWebView()
let request =&#34;https://www.google.fr&#34;



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(&#34;webview : frame :\(webView.frame), bounds : \(webView.bounds)&#34;)
    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) -&gt; Void) {
    println(&#34;decide policy action&#34;)
    decisionHandler(WKNavigationActionPolicy.Allow)
}
func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -&gt; Void) {
    println(&#34;decide policy response&#34;)
    decisionHandler(WKNavigationResponsePolicy.Allow)
}
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
    println(&#34;commit navigation&#34;)
}
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {
    println(&#34;fail in didFailNavigation \(error)&#34;)
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
    println(&#34;fail in didFailProvisionalNavigation \(error)&#34;)
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    println(&#34;finish navigation&#34;)
}
</code></pre>

<p>感谢阅读</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您永远不会将 <code>WKWebView</code> 添加到 View 层次结构中。在您的 <code>viewDidAppear</code> 中,哪些代码应该真正移动到 <code>viewDidLoad</code>,您可能想要替换:</p>

<pre><code>ViewForStuffInWeb = webView
</code></pre>

<p>与:</p>

<pre><code>ViewForStuffInWeb.addSubview(webView)
</code></pre>

<p>虽然不清楚 <code>ViewForStuffInWeb</code> 究竟是什么。仅当该 View 存在于您的 nib 中并且已连接时,上述操作才有效。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 UIView 中创建 WKWebView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32399066/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32399066/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 UIView 中创建 WKWebView