Ivaylo Nikolov
Ivaylo Nikolov

Reputation: 501

macOS WKWebView background transparency

If someone has experience with WKWebView, please share how to make the background of the view transparent. The WebView object has such option via var drawsBackground: Bool { get set } but it is missing for the WKWebView class. I searched the net and .. found nothing. Before time it was possible to do so via opaque property, but not anymore. There is a getter isOpaque ... and that's it. I don't want to do it via CSS and already tried everything else, like:

webview.wantsLayer = true

webview.layer?.backgroundColor = NSColor.clear.cgColor

If someone can help ...

I. Nikolov

Upvotes: 5

Views: 2051

Answers (2)

Ely
Ely

Reputation: 9131

This should work for both macOS 10.11 and macOS 10.12:

if NSAppKitVersion.current.rawValue > 1500 {
    webView.setValue(false, forKey: "drawsBackground")
}
else {
    webView.setValue(true, forKey: "drawsTransparentBackground")
}

Remark: this has been passed by App Store review.

Upvotes: 14

Heisenbug
Heisenbug

Reputation: 970

Your web page should have transparent background as well.

Upvotes: 0

Related Questions