【问题标题】:iOS13 WKWebView transparent backgroundiOS13 WKWebView透明背景
【发布时间】:2020-01-21 15:40:18
【问题描述】:

在 iOS12 下,以下代码在 WKWebView 中显示透明背景。

但是,在 iOS13 下,情况不再如此——为什么?以及如何再次实现透明背景?

我试过了

@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)
}

添加以下内容没有帮助...

webView.isOpaque = false
webView.backgroundColor = .clear

奇怪的是,如果我设置透明以外的背景颜色,那么它会起作用:

即通过替换以下内容,我得到了红色背景:

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

为什么transparent 不起作用??? (我也试过clear- 但没有成功)

【问题讨论】:

  • 刚刚遇到了同样的问题。除了它不只是 iOS 13,而是与 Xcode 11 有关。使用 Xcode 10 构建的应用程序在具有透明背景的 iOS 13 上运行(即 WKWebView 之一)。如果我使用 Xcode 11 构建应用程序,它在 iOS 11 上具有透明背景,但在 iOS 13 上没有。
  • 好的,再调查一下。 Xcode 11 + iOS 13 模拟器 = 没有透明背景。 Xcode 11 + 运行 iOS 13 的真实设备 = 透明背景。
  • 好的,更多信息。我的真实设备实际上运行的是 13.1,但模拟器仅适用于 13.0。
  • 在真实设备上试用 13.0 - 可重现,13.1 - 不可重现。 Apple 方面的许多错误之一,很确定在代码方面没有什么可以解决的。

标签: html uiwebview wkwebview ios13 swift5.1


【解决方案1】:

将复制我的评论作为答案。这是 Apple 自己的 iOS 13 错误。他们用 13.1 修复了它。我在运行 13.0 的同一设备上复制了它,然后将 iOS 更新到 13.1,问题就消失了。无法在代码中做任何事情来解决这个问题,只希望所有用户都从 13.0 更新到 13.1。

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2021-09-20
    • 2019-02-07
    • 2020-10-15
    相关资源
    最近更新 更多