UIWebView PaginationMode always show white background, how to make it transparent?

混江龙づ霸主 提交于 2019-12-21 02:43:31

问题


I try to use 'paginationMode' to make my html-content pagination for iOS 7 latter.

//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];

but in the end the web view always shows white background, how can I make it transparent?


回答1:


Your probably want to provide content background. But the technique of setting webview transparent and providing background color to view behind this may not work well when we use pagination left to right.

I solved this issue by providing content background through CSS and cater the issue of blank area at the end of html content with the help of this post

    - (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];
}

And call this method when webview finish loading content.




回答2:


Maybe try to set the background of the scrollView to clear ?

webView.scrollView.backgroundColor = [UIColor clearColor];

EDIT Ok so it seems that after setting

UIWebPaginationMode

The background become white behind :/




回答3:


In case of paginationmode, it's not really working directly from objective c code. However, I got my job done by injecting CSS for 'body' tag:

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];

Note : its for UIWebPaginationModeLeftToRight..



来源:https://stackoverflow.com/questions/27031092/uiwebview-paginationmode-always-show-white-background-how-to-make-it-transparen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!