ios 如何制作透明的 UIWebView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3646930/
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
How to make a transparent UIWebView
提问by Knodel
I have an app with a UITableView
and a corresponding detail view for each row. In the detail view I have to display some text and a background image (text is different for each row, but the image remains the same). The easiest way, in my opinion, is to put the text in an .rtf file and display it in a UIWebView
. Then just put a UIImageView
behind the UIWebView
.
我有一个应用程序,UITableView
每行都有一个和相应的详细信息视图。在详细视图中,我必须显示一些文本和背景图像(每行的文本不同,但图像保持不变)。在我看来,最简单的方法是将文本放在 .rtf 文件中并以 .rtf 格式显示UIWebView
。然后只需UIImageView
在UIWebView
.
I've tried to set the UIWebView
's opacity to zero in IB, but it didn't help.
我试图UIWebView
在 IB中将's opacity 设置为零,但它没有帮助。
Can you help me?
你能帮助我吗?
回答by Ortwin Gentz
I recommend:
我建议:
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
(setting these properties in Interface Builder will work for iOS 5.0+, but for iOS 4.3 you must set the backgroundColor in code)
(在 Interface Builder 中设置这些属性适用于 iOS 5.0+,但对于 iOS 4.3,您必须在代码中设置 backgroundColor )
And include this into your HTML code:
并将其包含在您的 HTML 代码中:
<body style="background-color: transparent;">
回答by Nilesh Kikani
Use the recursive method below to remove gradient from UIWebView:
使用下面的递归方法从 UIWebView 中删除渐变:
[webView setBackgroundColor:[UIColor clearColor]];
[self hideGradientBackground:webView];
- (void) hideGradientBackground:(UIView*)theView
{
for (UIView * subview in theView.subviews)
{
if ([subview isKindOfClass:[UIImageView class]])
subview.hidden = YES;
[self hideGradientBackground:subview];
}
}
回答by kmiklas
Swift update:
迅捷更新:
webView.opaque = true
webView.backgroundColor = UIColor.clearColor()
And again, don't forget to set
再次,不要忘记设置
<body style="background-color: transparent">
Or better still, instead of an inline style, in your style sheet:
或者更好的是,在您的样式表中,而不是内联样式:
body {
background-color: transparent
}
回答by Museer Ahamad Ansari
For Swift 3 and Xcode 8
对于 Swift 3 和 Xcode 8
self.webView.isOpaque = false;
self.webView.backgroundColor = UIColor.clear
回答by Pyae Thu Aung
In XCode 6.x uncheck Opaqueand change Background's opacityto 0%. I think other XCode versions will also work.
在 XCode 6.x 中取消选中Opaque并将背景的不透明度更改为0%。我认为其他 XCode 版本也可以使用。
回答by Xavier John
I was able to make my UIWebView transparent by going to 'Attributes Inspector' and unchecking Drawing Opaque.
通过转到“属性检查器”并取消选中“绘图不透明”,我能够使我的 UIWebView 透明。
My HTML code just for reference.
我的 HTML 代码仅供参考。
UIWebView* webView =(UIWebView *) [cell viewWithTag:100];
NSString* htmlContentString = [NSString stringWithFormat:
@"<html>"
"<style type='text/css'>html,body {margin: 0;padding: 0;width: 100%%;height: 100%%;}</style>"
"<body>"
"<table style='border:1px solid gray; border-radius: 5px; overflow: hidden;color:white;font-size:10pt' cellspacing=\"0\" cellpadding=\"1\" align='right'><tr>"
"<td>Hello</td><td>There</td>"
"</tr></table>"
"</body></html>"
];
[webView loadHTMLString:htmlContentString baseURL:nil];
回答by damithH
To remove the scrolls and make the UIWebView
transparent, try this code below:
要移除卷轴并使其UIWebView
透明,请尝试以下代码:
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
for(UIView *view in webView.subviews){?
? ? ?if ([view isKindOfClass:[UIImageView class]]) {
? ? ?? ? ?// to?transparent?
? ? ?? ? ?[view removeFromSuperview];
? ? ?}
? ? ?if ([view isKindOfClass:[UIScrollView class]]) {
? ? ?? ? ?UIScrollView *sView = (UIScrollView *)view;
? ? ?? ? ?//to hide Scroller bar
? ? ?? ? ?sView.showsVerticalScrollIndicator = NO;
sView.showsHorizontalScrollIndicator = NO;
? ? ?? ? ?for (UIView* shadowView in [sView subviews]){
? ? ?? ? ?? ? ?//to remove shadow
? ? ?? ? ?? ? ?if ([shadowView isKindOfClass:[UIImageView class]]) {
? ? ?? ? ?? ? ?? ? ?[shadowView setHidden:TRUE];
? ? ?? ? ?? ? ?}
? ? ?? ? ?}
? ? ?}
}
回答by yo2bh
In Swift 4.x,
在 Swift 4.x 中,
webView.backgroundColor = .clear
openSourceLicensesWebView.isOpaque = false