xcode 可可:如何使背景透明的 webView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10183857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cocoa: How to make background transparent webView?
提问by u1334703
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
回答by Rob Keniger
You need to call setDrawsBackground:
on the WebView
and pass in NO
. That will prevent the web view from drawing a background unless the page loaded in the web view explicitly sets the background color.
你需要调用setDrawsBackground:
的WebView
,并传递NO
。这将阻止 web 视图绘制背景,除非 web 视图中加载的页面明确设置背景颜色。
回答by Sumit Shastri
We can add background image in WebView using cocoa. to do this we need to add css for body tag.
我们可以使用可可在 WebView 中添加背景图像。为此,我们需要为 body 标签添加 css。
Create a webview:
创建一个网络视图:
WebView *webView;
Fetch image form bundle path:
获取图像形式包路径:
NSString *path = [[NSBundle mainBundle] pathForResource:@"my-bg" ofType:@"jpg"];
write Css for body tag.
为 body 标签编写 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 的背景中。