ios UIWebView 背景设置为Clear Color,但不透明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8667150/
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
UIWebView background is set to Clear Color, but it is not transparent
提问by VansFannel
I'm developing an iOS 4 application using iOS SDK latest version and XCode 4.2.
我正在使用 iOS SDK 最新版本和 XCode 4.2 开发 iOS 4 应用程序。
I have a XIB with a UIWebView
with Alpha = 1.0, Backgroundset to Clear Colorand Opaqueis not set. On this XIB I setup an image as background with this code:
我有一个UIWebView
带有Alpha = 1.0的 XIB ,背景设置为清除颜色并且未设置不透明。在此 XIB 上,我使用以下代码将图像设置为背景:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"AboutBackground.png"]];
self.view.backgroundColor = background;
[background release];
}
return self;
}
The UIWebView
is showing an static html:
在UIWebView
是否显示静态的html:
<html><head></head><body style=\"margin:0 auto;text-align:center;background-color: transparent; color:white\">...</body></html>
On iOS 5 simulator, its background is transparent, but on an iOS 4 device is grey.
在 iOS 5 模拟器上,它的背景是透明的,但在 iOS 4 设备上是灰色的。
Any clue?
有什么线索吗?
回答by Maulik
Also set :
还设置:
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
回答by dheerendra
/*for ios please set this*/
[webViewFirst setOpaque:NO];
/*for html please set this*/
<body style="background:none">
回答by Elegia
Besides setting your webview's background to clear color, also make sure that you set opaque to false.
除了将 webview 的背景设置为清除颜色之外,还要确保将 opaque 设置为 false。
回答by Artem Stepanenko
You can try this code (i know, that it's unsafe, but it works even for ios5):
你可以试试这个代码(我知道,它不安全,但它甚至适用于 ios5):
- (void)makeBodyBackgroundTransparent {
for (UIView *subview in [webView subviews]) {
[subview setOpaque:NO];
[subview setBackgroundColor:[UIColor clearColor]];
}
[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor clearColor]];
}
回答by Avinash651
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
回答by user5742417
NSString *content=@"example clear background color UIWebview";
NSString *style=[NSString stringwithformat:@"<html><head><style>body {background-color:transparent;}</style></head><body>%@</body></html>",content];
[myWebview loadhtmlstring:style baseurl:nil];
回答by Amr Angry
For Objective
为目标
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
Please make sure to include this into your HTML code:
请确保将其包含在您的 HTML 代码中:
<body style="background-color: transparent;">
or
或者
<body style="background:none">
回答by RichAppz
Latest Swift Version (as required):
最新 Swift 版本(根据需要):
lazy var webView: UIWebView = {
let view = UIWebView()
view.delegate = self
view.backgroundColor = .clear
view.isOpaque = false
return view
}()
Remove delegate line if not required
如果不需要,请删除委托行