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.

+3


source to share


3 answers


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.

+23


source


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 yours WKWebView

is optional, so you may not have started it.

+15


source


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
}

      

0


source







All Articles