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.
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.
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.
So a WKCompositingView is a host-defined level in the tree of composited HTML content that participates in drawing and hit testing.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With