0

我正在尝试将 WKWebView 添加为子视图:

func loadWebView() {

    let userAgentStr = self.getUserAgent()
    if let url = URL(string: videoURL.stringValue){
        let request  = URLRequest.init(url: url)
        let config = WKWebViewConfiguration()
        self.webView = WKWebView(frame: self.view.frame, configuration: config)
        self.webView.wantsLayer = true
        self.webView.customUserAgent = userAgentStr
        self.webView.load(request)
        self.webView.layer?.backgroundColor = NSColor.green.cgColor
        self.view.addSubview(self.webView)
        self.view.needsDisplay = true

    }
}

但没有显示在主 NSView 中。但已添加为子视图:

po self.view.subviews
▿ 9 elements
  - 0 : <NSTextField: 0x10050e400>
  - 1 : <NSTextField: 0x10050f7f0>
  - 2 : <NSTextField: 0x10050e8e0>
  - 3 : <NSButton: 0x600003510000>
  - 4 : <NSButton: 0x6000035100b0>
  - 5 : <NSTextField: 0x10050ee00>
  - 6 : <NSButton: 0x600003510160>
  - 7 : <NSView: 0x6000033054a0>
  - 8 : <WKWebView: 0x600003e01400>

我试图看看我是否做错了什么并将其添加为 NSView 作为子视图:

func loadNSView() {
    let newView = NSView()
    newView.frame = self.view.frame
    newView.wantsLayer = true
    newView.layer?.backgroundColor = NSColor.white.cgColor
    self.view.addSubview(newView)
    self.view.needsDisplay = true
}

它工作得很好。

你们中的任何人都知道我可能做错了什么吗?或者我该怎么做才能加载 WKWebView ?

我会非常感谢你的帮助。

4

1 回答 1

0

我找到了这个问题的根源。由于是 MacOS (Cocoa) 应用程序,您需要启用传入和传出连接:

在此处输入图像描述

于 2019-04-12T17:09:24.773 回答