Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I give WKWebView a colored background

I'm working on an app in which I load different html files with mostly dark backgrounds. Right now there's a small white flash when navigating from one page to another, presumably since the next page has not loaded yet. I'd like to get rid of that flash and thought the most straightforward way would be to give the WebView a background color.

I tried setting the color for the WebView as well as the scrollView inside of it, but that doesn't seem to work:

self.webView?.backgroundColor = UIColor.blackColor() self.webView?.scrollView.backgroundColor = UIColor.blackColor() 

I see a flash of the color when the view is loaded the first time, but not on subsequent navigation.

like image 473
chicken Avatar asked Dec 26 '14 10:12

chicken


People also ask

How to change WKWebView background color?

Click on the WKWebView in soryboard, and choose preferred Background color from attributes inspector panel. You can also instead uncheck the Opaque checkbox for the WKWebView in storyboard.

How do I change the background color in react native Webview?

No, you cannot change the background color of a webview, but you can use another view with your background color cover on your web view.

What is WKCompositingView?

So a WKCompositingView is a host-defined level in the tree of composited HTML content that participates in drawing and hit testing.


2 Answers

To stop 'white flash' on your dark background, do this

webView.opaque = false 

This doesn't really solve background colour issue, but at least it stops 'white flash' you are experiencing. Apparently there seem to be no way to change background colour of WKWebView before loading HTML on it.

Swift 4

webView.isOpaque = false 
like image 145
user5865651 Avatar answered Sep 18 '22 14:09

user5865651


self.webView = WKWebView() self.webView.backgroundColor = UIColor(red:0.11, green:0.13, blue:0.19, alpha:1) self.webView.scrollView.backgroundColor = UIColor(red:0.11, green:0.13, blue:0.19, alpha:1) 

Don't forget that your WKWebView is an optional so maybe you don't init it.

like image 43
BilalReffas Avatar answered Sep 18 '22 14:09

BilalReffas