Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a transparent UIWebView

People also ask

How do you make a view transparent in Xcode?

xib, you simply do it in interface builder by selecting the option "Clear Color" for the Background of the view in the Utilities Pane (the pane on the right). "Clear Color" will give the view a completely transparent background.

How do I make a transparent view in SwiftUI?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.


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)

And include this into your HTML code:

<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];
  }
}

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
}

For Swift 3 and Xcode 8

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

In XCode 6.x uncheck Opaque and change Background's opacity to 0%. I think other XCode versions will also work.enter image description here


I was able to make my UIWebView transparent by going to 'Attributes Inspector' and unchecking Drawing Opaque.

My HTML code just for reference.

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