I have been asked many times how to set a background color for WKWebView. I have got also many comments that it cannot be set. I have used the following code which gives the background a nice dark gray color.

let webView: WKWebView = {
let webView = WKWebView()
    webView.isOpaque = false
    webView.backgroundColor = UIColor(red: 40/255, green: 40/255, blue: 40/255, alpha: 1)
    webView.scrollView.backgroundColor = UIColor(red: 40/255, green: 40/255, blue: 40/255, alpha: 1)
    webView.translatesAutoresizingMaskIntoConstraints = false
    return webView
}()

 

Leave a Comment

Your email address will not be published.