我制作了一种使用UIWebView的阅读器。我想包括一个夜间模式功能,它将文本白色和背景黑色,而不是白色背景上的普通黑色文本。UIWebView上的夜间模式问题
为此,我使用下面的代码:
if (nightMode == YES)
{
[self.view setBackgroundColor:[UIColor whiteColor]];
NSString *setJavaScript = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'; document.getElementsByTagName('html')[0].style.backgroundColor= 'white'; DOMReady();"];
[readerWebView stringByEvaluatingJavaScriptFromString:setJavaScript];
[readerWebView.scrollView setBackgroundColor:[UIColor whiteColor]];
[[NSUserDefaults standardUserDefaults] setBool:(NO) forKey:@"nightMode"];
nightMode = NO;
}
else
{
[self.view setBackgroundColor:[UIColor blackColor]];
NSString *setJavaScript = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'; document.getElementsByTagName('html')[0].style.backgroundColor= 'black'; DOMReady();"];
[readerWebView stringByEvaluatingJavaScriptFromString:setJavaScript];
//[readerWebView setBackgroundColor:[UIColor blackColor]]; // doesn't solve it
[readerWebView.scrollView setBackgroundColor:[UIColor blackColor]];
[[NSUserDefaults standardUserDefaults] setBool:(YES) forKey:@"nightMode"];
nightMode = YES;
}
[[NSUserDefaults standardUserDefaults] synchronize];
大部分的时间这个作品找到我得到的结果我的预期。但有时它看起来像这样:
那些在UIWebView中设置页边距保持白色的时候应该是黑色或黑色时,他们应该是白色的。这似乎完全是随机的!尤其是因为它不在整个网页上,而是从1024px开始(正好是iPad屏幕的高度)。
有没有人有任何线索如何解决这个问题?
它既在模拟器中,也在真实的iPad上。
你有没有打过电话[readerWebView setNeedsDisplay]? –
不好意思,不幸 – Mathijs
这是发生在模拟器还是实际设备?有可能是一个模拟器的错误...这只是一个猜测 –