140

I'm developing an iOS 4 application using iOS SDK latest version and XCode 4.2.

I have a XIB with a UIWebView with Alpha = 1.0, Background set to Clear Color and Opaque is not set. On this XIB I setup an image as background with this code:

- (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:

<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.

Any clue?

Venk
  • 5,929
  • 9
  • 38
  • 50
VansFannel
  • 43,504
  • 101
  • 342
  • 588
  • 1
    possible duplicate of [How to make a transparent UIWebVIew](http://stackoverflow.com/questions/3646930/how-to-make-a-transparent-uiwebview) – Vladimir Jul 31 '12 at 13:44
  • try to just put an UIImageView on your webview in interface builder and set it your image. – Stas Dec 29 '11 at 11:17
  • Thanks for your answer, but the web view doesn't fill the entire screen. – VansFannel Dec 29 '11 at 11:25

8 Answers8

364

Also set :

[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
Maulik
  • 19,268
  • 14
  • 81
  • 136
  • 3
    This only works for me if I set the background color and opaqueness after calling loadHTMLString. It seems like loadHTMLString resets both of those values. – Justin Anderson Sep 30 '12 at 15:05
  • 1
    I know the question was for iOS, but if for OSX, this [question and answer](http://stackoverflow.com/a/34525808/105539) resolves the problem and may also help those with iOS too. – Volomike Dec 30 '15 at 20:21
  • This solution is still working in iOS 12.1, Xcode 10.1 and Swift. – Bhavin_m Apr 10 '19 at 12:36
15
     /*for ios please set this*/

     [webViewFirst setOpaque:NO];

     /*for html please set this*/
     <body style="background:none">
dheerendra
  • 527
  • 6
  • 8
9

Besides setting your webview's background to clear color, also make sure that you set opaque to false.

Elegia
  • 325
  • 2
  • 9
3
webView.opaque = NO;

webView.backgroundColor = [UIColor clearColor];
NeverHopeless
  • 10,869
  • 4
  • 34
  • 55
Avinash651
  • 1,369
  • 10
  • 17
3
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];
Manjunath Ballur
  • 6,091
  • 3
  • 35
  • 45
3

You can try this code (i know, that it's unsafe, but it works even for ios5):

- (void)makeBodyBackgroundTransparent {
        for (UIView *subview in [webView subviews]) {
            [subview setOpaque:NO];
            [subview setBackgroundColor:[UIColor clearColor]];
        }
        [webView setOpaque:NO];
        [webView setBackgroundColor:[UIColor clearColor]];
}
Artem Stepanenko
  • 3,273
  • 5
  • 26
  • 51
2

Latest Swift Version (as required):

lazy var webView: UIWebView = {
    let view = UIWebView()
    view.delegate = self
    view.backgroundColor = .clear
    view.isOpaque = false
    return view
}()

Remove delegate line if not required

RichAppz
  • 1,480
  • 2
  • 14
  • 25
1

For Objective

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

Please make sure to include this into your HTML code:

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

or

 <body style="background:none">
Amr Angry
  • 3,512
  • 1
  • 43
  • 36