繁体   English   中英

cocoa:如何使背景透明webView?

[英]cocoa: How to make background transparent webView?

mac osx,我想让我的webview看起来透明,并在其上显示数据,背景显示父视图的图像。

谁能告诉我怎么做? 这种方法不行

NSString * source = @"<html><head><title></title></head><body><div id=\"box\">Example Box</div></body></html>";
[[webView mainFrame] loadHTMLString:source baseURL:[NSURL URLWithString:@""] ];
[[webView windowScriptObject] evaluateWebScript:@"document.getElementById('box').style.backgroundColor = '#dddddd';"]; // Change box background
[[webView windowScriptObject] evaluateWebScript:@"document.body.style.backgroundColor = '#dddddd';"]; // Change body background

您需要在WebView上调用setDrawsBackground:并传入NO 这将阻止Web视图绘制背景,除非Web视图中加载的页面明确设置背景颜色。

我们可以使用cocoa在WebView中添加背景图像。 要做到这一点,我们需要为body标签添加css。

创建一个webview:

WebView *webView;

获取图像表单包路径:

NSString *path = [[NSBundle mainBundle] pathForResource:@"my-bg" ofType:@"jpg"];

为身体标签写Css。

NSMutableString *htmlBuilder = [[NSMutableString alloc] init];
[htmlBuilder appendString:@"body {"];
[htmlBuilder appendString:[NSString stringWithFormat:@" background-image: url('file://%@');",path]];
[htmlBuilder appendString:@"}"];

在webview上加载此图像

[[webView mainFrame] loadHTMLString:htmlBuilder baseURL:nil];
[webView setDrawsBackground:NO];

您的图片将在webview的背景中添加。

暂无
暂无

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

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