1

Under iOS12 the following code showed a transparent background in a WKWebView.

However, under iOS13 this is no longer the case - why ? And how to achieve the transparent background back again ?

I tried

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.scrollView.bounces = false
    webView.navigationDelegate = self

    let body =
        "<html>\n" +
            "<head>\n" +
                "<style>\n" +
                    "html { margin: 0; }" +
                    "body { margin: 0; font-family: Avenir-Light; font-size: 41; color: white; background-color: transparent }\n" +
                    "a, a:active, a:visited, a:hover { color: #FFED00 }\n" +
                "</style>\n" +
            "</head>\n" +
            "<body>\n" +
            article.body +
            "</body>\n" 
        "</html>"

    webView.loadHTMLString(body, baseURL: nil)
}

Adding the following does not help...

webView.isOpaque = false
webView.backgroundColor = .clear

And strangely enough, if I set a background-colour other than transparent, then it works:

i.e. by replacing the following, I get a red background:

"body { margin: 0; font-family: Avenir-Light; font-size: 41; color: white; background-color: red }\n"

Why is transparent not working ??? (I also tried clear- but no success)

iKK
  • 6,394
  • 10
  • 58
  • 131
  • Just ran into the same problem. Except it's not just iOS 13, but something to do with Xcode 11. The application built with Xcode 10 runs on iOS 13 with a transparent background (WKWebView one that is). If I use Xcode 11 to build the app, it has a transparent background on, say, iOS 11 but not iOS 13. – NeverwinterMoon Sep 26 '19 at 08:49
  • OK, investigated a bit more. Xcode 11 + iOS 13 emulator = no transparent background. Xcode 11 + real device running iOS 13 = transparent background. – NeverwinterMoon Sep 26 '19 at 09:09
  • OK, more info. My real device is actually running 13.1 but the emulator is available only for 13.0. – NeverwinterMoon Sep 26 '19 at 09:22
  • Tried on real device with 13.0 - reproducible, 13.1 - not reproducible. One of many bugs on Apple side, pretty sure there is nothing to be done on the code side to fix that. – NeverwinterMoon Sep 26 '19 at 10:12

1 Answers1

3

Going to duplicate my comment as an answer. This is an Apple's own bug with iOS 13. They fixed it with 13.1. I reproduced it on the same device running 13.0, then updated iOS to 13.1 and the problem was gone. Nothing can be done in code to fix that and just hope that all the users have updated from 13.0 to 13.1.

NeverwinterMoon
  • 2,363
  • 21
  • 24