How can I give WKWebView a colored background

chicken picture chicken · Dec 26, 2014 · Viewed 17.4k times · Source

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.

Answer

user5865651 picture user5865651 · Feb 1, 2016

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