简体   繁体   English

cocoa:如何使背景透明webView?

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

The mac osx ,I want to make my webview to look transparent and show the data on it with background showing images of parent view. mac osx,我想让我的webview看起来透明,并在其上显示数据,背景显示父视图的图像。

Can anyone please let me know how to do this? 谁能告诉我怎么做? This method can not 这种方法不行

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

You need to call setDrawsBackground: on the WebView and pass in NO . 您需要在WebView上调用setDrawsBackground:并传入NO That will prevent the web view from drawing a background unless the page loaded in the web view explicitly sets the background color. 这将阻止Web视图绘制背景,除非Web视图中加载的页面明确设置背景颜色。

We can add background image in WebView using cocoa. 我们可以使用cocoa在WebView中添加背景图像。 to do this we need to add css for body tag. 要做到这一点,我们需要为body标签添加css。

Create a webview: 创建一个webview:

WebView *webView;

Fetch image form bundle path: 获取图像表单包路径:

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

write Css for body tag. 为身体标签写Css。

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

Load this image on webview 在webview上加载此图像

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

Your image will be added in background on webview. 您的图片将在webview的背景中添加。

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

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