UIWebView分页模式始终显示白色背景,如何使其透明



我尝试使用"分页模式"为后来的iOS 7进行html内容分页。

//set webview to transparent
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
// set multi-columns
webView.paginationBreakingMode = UIWebPaginationBreakingModePage;
webView.paginationMode = UIWebPaginationModeLeftToRight;
webView.pageLength = webView.bounds.size.width;
// set html to transparent background
NSString *transparent = @"addCSSRule('html', 'background-color: transparent')";
[webView stringByEvaluatingJavaScriptFromString:transparent];

但最终 Web 视图始终显示白色背景,如何使其透明?

您可能希望提供内容背景。但是,当我们使用从左到右分页时,将 webview 设置为透明并提供背景颜色以在其后面查看的技术可能效果不佳。

我通过CSS提供内容背景解决了这个问题,并在这篇文章的帮助下解决了html内容末尾空白区域的问题

    - (void)addExtraContentView
{
    UIWebView* wv = self.webView;
    if(!self.viewExtraContent)
    {
        NSString* width = [wv stringByEvaluatingJavaScriptFromString:@"document.documentElement.offsetHeight;"];
        CGFloat contentHeight = [width floatValue];
        CGFloat contentWidth = wv.scrollView.contentSize.width;
        CGFloat y = (int)contentHeight % (int)wv.frame.size.height;
        self.viewExtraContent = [[UIView alloc] initWithFrame:CGRectMake(contentWidth - wv.frame.size.width, y, wv.frame.size.width, (wv.frame.size.height))];
    }
    self.viewExtraContent.backgroundColor = [BookReaderSettings shared].backgroundColor;
    [wv.scrollView addSubview:self.viewExtraContent];
}

并在 webview 完成加载内容时调用此方法。

也许尝试将滚动视图的背景设置为清除?

webView.scrollView.backgroundColor = [UIColor clearColor];

编辑好的,所以似乎在设置之后

UIWebPaginationMode

背景在后面变成白色:/

在分页模式的情况下,它并没有真正直接从目标 c 代码工作。但是,我通过为"body"标签注入CSS来完成工作:

NSString* path=[[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
@"addCSSRule('body', 'background-color: transparent !important; background-image:url(%@); background-repeat: repeat-x; background-size:%fpx %fpx;')",[NSURL fileURLWithPath:path],webView.frame.size.width,webView.frame.size.height];

注意:它用于UIWebPaginationModeLeftToRight。

最新更新