How to make a transparent UIWebView

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.

I've tried to set the UIWebView's opacity to zero in IB, but it didn't help.

Can you help me?

58170 次浏览

我建议:

webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];

(将这些属性设置为 Interface Builder 对于 iOS 5.0 + 是可行的,但是对于 iOS 4.3,你必须设置 background Color 用密码)

并将其包含到 HTML 代码中:

<body style="background-color: transparent;">

Use the recursive method below to remove gradient from 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];
}
}

要删除卷轴并使 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];
               }
          }
     }
}

我可以让我的 UIWebView 透明,通过去“属性检查器”和取消选中绘图不透明。

我的 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];

快速更新:

webView.opaque = true
webView.backgroundColor = UIColor.clearColor()

And again, don't forget to set

<body style="background-color: transparent">

或者在样式表中更好地替代内联样式:

body {
background-color: transparent
}

在 XCode 6.x 中取消选中 不透明并将 背景不透明改为 0% 。我认为其他 XCode 版本也可以。enter image description here

For Swift 3 and Xcode 8

self.webView.isOpaque = false;
self.webView.backgroundColor = UIColor.clear

In Swift 4.x,

webView.backgroundColor = .clear
openSourceLicensesWebView.isOpaque = false