cocoa:如何使背景透明webView?

时间:2012-04-17 01:21:24

标签: xcode macos cocoa

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

2 个答案:

答案 0 :(得分:17)

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

答案 1 :(得分:-1)

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

创建网络视图:

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的背景中添加。

相关问题