简体   繁体   English

如何将 iOS 13 暗模式用于 wkwebview

[英]how to use iOS 13 darkmode for wkwebview

I was working for iOS 13 with Xcode 11 beta.我正在使用 Xcode 11 beta 为 iOS 13 工作。 Is there any way to support dark mode on web views?有什么方法可以在网页视图上支持暗模式? I have created a color set for all the other views except WKWebviews.我为除 WKWebviews 之外的所有其他视图创建了一个颜色集。 How to change web view background and text color for dark mode?如何更改深色模式的 Web 视图背景和文本颜色?

Assuming your question is asking how to change the colors of the HTML content you are displaying in a WKWebView based on whether light or dark mode is in effect, there is nothing you do in your app's code.假设您的问题是询问如何根据明暗模式是否有效来更改您在 WKWebView 中显示的 HTML 内容的颜色,那么您在应用程序的代码中没有任何操作。 All changes need to be in the CSS being used by your HTML content.所有更改都需要在您的 HTML 内容使用的 CSS 中进行。

I have some local HTML files I use in a WKWebView.我在 WKWebView 中使用了一些本地 HTML 文件。 I was able to support dark mode by updating my css file.我能够通过更新我的 css 文件来支持暗模式。

Let's say you currently have a css file with the following:假设您目前有一个包含以下内容的 css 文件:

body {
    background-color: white;
    color: black;
}

a:link {
    color: #0078b5;
    text-decoration: none;
}

Those are fine in light mode.这些在浅色模式下很好。 To also support dark mode, you can add an @media section to your css:为了也支持暗模式,你可以在你的 css 中添加一个@media部分:

@media (prefers-color-scheme: dark) {
    body {
        background-color: rgb(38,38,41);
        color: white;
    }
    a:link {
        color: #0096e2;
    }
    a:visited {
        color: #9d57df;
    }
}

When in dark mode, the colors in this @media section will override the corresponding colors defined outside the @media section.在暗模式下,此@media部分中的颜色将覆盖在@media部分之外定义的相应颜色。

Same challenge I faced when I was migrating my iOS app because we do login using WKWebView and when I consulted I found below example to handle this situation.我在迁移 iOS 应用程序时面临同样的挑战,因为我们确实使用WKWebView登录,当我咨询时,我发现下面的示例可以处理这种情况。 Just need to create variable for the color and need to handle this in CSS.只需要为颜色创建变量并需要在 CSS 中处理它。

Before

body { color: black; }
h1 { color: white; }
.header {
    background-color: #FFFFFF;
    color: white;
}

After

:root {
    color-scheme: light dark;
        --h1-color: #333;
        --header-bg-clr: #FFF1FF;
        --header-txt-clr: white;
    }
    @media (prefers-color-scheme: dark) {
    :root {
        color-scheme: light dark;
        --h1-color: #333;
        --header-bg-clr: #FFF1FF;
        --header-txt-clr: white;
        }
    }

body { }
h1 { color: var(--h1-color); }
.header {
    background-color: var (--header-bg-clr);
    color: var(--header-txt-clr);
}

After Integrating this change you can use Safari to test (First you need to enable the developer menu option in Sarafi, Preferences, Advanced).集成此更改后,您可以使用 Safari 进行测试(首先您需要在 Sarafi、首选项、高级中启用开发人员菜单选项)。 Open wen inspector (using Command + Options + I) and you will see this screen with the option to toggle light/dark mode.打开 wen 检查器(使用 Command + Options + I),您将看到此屏幕,其中包含切换亮/暗模式的选项。

在此处输入图片说明

NOTE Just to add little more information.注意只是为了添加更多信息。 You can also handle different images just like colors.您还可以像处理颜色一样处理不同的图像。

BEFORE

<img src="day.jpg">

AFTER

<picture>
<source srcset="light.jpg" media="(prefers-color-scheme: light)">
<img src="day.jpg">
</picture>

Swift 5斯威夫特 5

For WKWebView , below code worked for me.对于WKWebView ,下面的代码对我WKWebView

extension RichTextController : WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        let cssString = "@media (prefers-color-scheme: dark) {body {color: white;}a:link {color: #0096e2;}a:visited {color: #9d57df;}}"
        let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
        webView.evaluateJavaScript(jsString, completionHandler: nil)
    }
}

More simple, just invert all colors & style except for images :更简单,只需反转除图像以外的所有颜色和样式:

@media (prefers-color-scheme: dark) {
    html{
        filter: invert(1)  hue-rotate(.5turn);
    }
    img {
        filter: invert(1)  hue-rotate(.5turn);
    }
}

Root tag will invert all the components color except the table and images will be in negative form.根标签将反转除表格和图像以外的所有组件颜色。

To perform perfect color invert check the below CSS file要执行完美的颜色反转,请检查以下 CSS 文件

   @media (prefers-color-scheme: dark) {
     /* root tag inverting all the components color except the table.*/
     : root {
            color-scheme: light dark;
        filter: invert(100%);
       -webkit-filter: invert(100%)
     }
    /*table tag needed for inverting table content.*/
    table {
            filter: invert(100%);
     }
    /* If We do color invert in : root , images will be color inverted and it will be in negative. If we invert again these negative images, it will be positive.*/
     img {
         filter: invert(100%);
     }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM