简体   繁体   中英

How to get WebView (or WKWebView) on OSX to adopt Dark Mode in Mojave

I have an embedded WebView in my Mac app. I use it to display HTML text (generated from Markdown.)

I'm trying to make it adopt Dark Mode in Mojave (similar to the way an NSTextView does 'out the box'.)

Is this possible - or do I need to manually insert some CSS stuff into the HTML to specifically set the colors?

Assuming it's the latter - does anyone know how to do this in a generic way (ie get the CSS to detect whether it needs to be dark or light based on the client browser.)

Thanks in advance...

When you switch to dark mode, all standard views switch to dark mode and you don't have to do anything.

This said, the WKWebView is showing "content", and "content" should not be changed by the System, "content" should show itself with its own colors. If you show a web site, there is no reason the System should change the colors.

But if you have a use case where it makes sense to updated your view content colors to the System mode, then I guess your best option is to detect change to dark/light mode and reload the CSS styles.

Detecting the change can be done by overriding you view viewDidChangeEffectiveAppearance method.

- (void)    viewDidChangeEffectiveAppearance
{
    [super viewDidChangeEffectiveAppearance] ;
    NSLog(@"New appearance is %@",[self effectiveAppearance].name) ;
}

Based on the appearance name ( NSAppearanceNameVibrantDark or NSAppearanceNameVibrantLight ) you can load a CSS or another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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