How can I give WKWebView a colored background
I am working on an application where I load different html files with dark backgrounds. Right now, there is a small white flash when navigating from one page to another, presumably since the next page hasn't loaded yet. I would like to get rid of this flash and thought that the simplest way would be to give the WebView a background color.
I tried to set the color for the WebView as well as the scrollView inside it, but that doesn't seem to work:
self.webView?.backgroundColor = UIColor.blackColor()
self.webView?.scrollView.backgroundColor = UIColor.blackColor()
I see a flash of color when the view is loaded for the first time, but not on subsequent navigation.
source to share
To stop the "white flash" on your dark background, do this
webView.opaque = false
This doesn't really fix the background color problem, but at least it stops the "white flash" you're experiencing. Apparently there seems to be no way to change the background color of the WKWebView before the HTML is loaded on it.
source to share
I used to hide the WKWebView when I did a download request in the same project.
webView.isHidden = true
webView.load(req)
When the download was complete, I set the webView visibility again.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
videoView.isHidden = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
activityIndicator.stopAnimating()
videoView.isHidden = false
}
source to share