WKWebView Transparent Background (NSView)

I am building a Mac application using Swift. Therefore, I want to make WKWebView transparent, so it displays the text of the loaded HTML, but the background of my underlying NSWindow is visible. I tried

webView.layer?.backgroundColor = NSColor.clearColor().CGColor; 

which had no effect. WKWebView inherits from NSView, but I don't know if this helps.

Another solution would be to insert NSVisualEffectView as the background of the WebView, but I don't know how to do it, either!

+7
swift appkit macos
source share
3 answers

It was not supported, then they fixed it:

https://bugs.webkit.org/show_bug.cgi?id=134779

A way to make it transparent:

 myWebView.opaque = false 
+8
source share

The code below works fine for me, and the default color is set to clearColor.

 [wkWebView setValue:YES forKey:@"drawsTransparentBackground"]; 
+3
source share

Use this on macOS 10.12 and higher:

 webView.setValue(false, forKey: "drawsBackground") 
+1
source share

All Articles